Do you really understand the several states of threads

Introduction to thread status

Here we are talking about thread status in Java.

The thread status is as follows:

  • Initial state of thread:NEW

  • Thread running status:RUNNABLE

  • Thread blocking state:BLOCKED

  • Thread wait state:WAITING

  • Timeout waiting state:TIMED_WAITING

  • Thread termination status:TERMINATED

The waiting state should be a more complex and important state.

The thread enters the waiting state, that is, the thread gives up the right to use the CPU for some reason, and blocking is also divided into several situations:

  • Waiting for blocking: running thread execution waitmethod, JVM will put the current thread into the waiting queue

  • Synchronous blocking: When the running thread acquires the synchronization lock of the object, if the synchronization lock is occupied by other thread locks, then the JVM will put the current thread into the lock pool

  • Other blocking: When a running thread executes Thread.sleepor a joinmethod, or an I/O request is issued, the JVM will set the current thread to a blocked state. When the sleepend jointhread terminates and the I/O processing is completed, the thread resumes

The transition between thread states is as follows:

Below I will explain which situations will enter these states.

The state of the thread at sleep

operation result

new t1 t1 的状态:NEW
t1 running is false,t1将sleep
t1.sleep()时的状态:TIMED_WAITING

Let's analyze when new Thread, the thread t1 [Thread-0] status is NEW. The thread starts, executes the run()method, and prints t1 running is false,t1将sleep. At this time, the thread t1 sleeps. Then the main thread sleeps and the variable is runningset to false. This thread t1 is still sleeping. Put the main thread to sleep again, and thread t1 is still sleeping. The state of thread t1 at this time is TIMED_WAITING.

If we modify the sleep time in thread t1

Thread.sleep(10000L);  -> Thread.sleep(3000L);

Let's take a look at the results

new t1 t1 的状态:NEW
t1 running is false,t1将sleep
t1.sleep()时的状态:TERMINATED

Thread t1 is terminated, so when you look at the code, don't think that it sleep()is a timeout waiting [TIMED_WAITING] state. To see if the thread has terminated.

The state when the thread joins

The results are as follows

t2中执行t1.join(5000L)
t2的状态:TIMED_WAITING
t2中执行t1.join()
t2的状态:WAITING
t2执行完

When executed

t1.start();
t2.start();

Thread t1 sleeps, enters thread t2 at this time, and executes t2中执行t1.join(5000L). Next t1 will preempt, enter the main thread, and the main thread sleeps. At this time, t2 is still waiting for t1, so the thread state of t2 is [TIMED_WAITING]. At this time, the main thread sleeps again, and execution starts in t1.join()t2. At this time, the state of t2 is [WAITING].

The state of the thread when it is synchronized

Let's see the results

t1抢不到锁的状态:BLOCKED
t1抢到锁

The main thread starts, and first grabs the lock. At this time, t1.start()the t1 thread is started. At this time, the main thread sleeps and the lock has not been released. The state of t1 at this time is [BLOCKED].

The state of the thread when it is wait

operation result

t1将wait(1000L)
t1的状态:TIMED_WAITING
t1的状态:BLOCKED
t1将wait
t1的状态:WAITING
t1将执行完
t1的状态:RUNNABLE
t1的状态:TERMINATED

The main thread starts, executes t1.start(), enters t1, executes t1将wait(1000L), and then t1 gives up the lock. While waiting for timeout at t1, the main thread sleeps. After that, the main thread here grabs the lock, and the state of t1 is [TIMED_WAITING]. At this time, the main thread executes object.notify(), but the lock has not been released yet, and t1 has not yet acquired the lock, so the state of t1 is [BLOCKED]. After the main thread releases the lock, t1 obtains the lock and executes object.wait(), then the state of t1 is [WAITING]. Then return to the main thread, obtain the lock, and execute object.notify(). At this time, the t1 thread is awakened and is in the running state [RUNNABLE]. The execution of t1 is completed, and the status is [TERMINATED].

State of thread park()

t1 park后的状态:WAITING
t1 unpark后的状态:WAITING

You can think about why thread t1 is in this state, and I will write an LockSupportarticle if I have the opportunity .

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/108506393