Java线程状态简介及状态间的转换

Java中实现多线程的方式有两种:1、继承Thread类;2、实现Runnable接口。线程的状态一共有六种:

1、初始(NEW):新建一个线程后,调用start()方法前线程处于初始状态;

2、可运行状态(RUNNABLE):调用start()方法后,线程处于JVM中,等待获取操作系统资源如CPU等;

3、阻塞状态(BLOCKED):线程等待锁的状态。等待获取锁进入同步代码块/方法或调用wait()方法后重新进入需要竞争锁;

4、等待状态(WAITING):一个线程在等待另一个线程执行动作时。调用以下方法线程进入该状态:Object.wait(),Object.join(),LockSupport.park()。处于该种状态的线程是不会自动唤醒的,必须等待另一个线程调用notify()或notifyAll()方法才能唤醒。

5、等待指定时间(TIMED_WAITING):线程等待指定时间,时间结束后自动唤醒并进入竞争锁的队列。调用一下方法进入该状态:Thread.sleep(long)、Object.wait(long)、Thread.join(long)、LockSupport.parkNanos()、LockSupport.parkUntil()

6、终止状态(TERMINATED):线程执行完毕或异常时。

 

Java线程状态之间的转换如下图:

 

 

Java线程状态源码(JDK1.8)

 1 /**
 2      * A thread state.  A thread can be in one of the following states:
 3      * <ul>
 4      * <li>{@link #NEW}<br>尚未启动的线程处于这种状态
 5      *     A thread that has not yet started is in this state.
 6      *     </li>
 7      * <li>{@link #RUNNABLE}<br>在Java虚拟机中执行的线程处于这种状态
 8      *     A thread executing in the Java virtual machine is in this state.
 9      *     </li>
10      * <li>{@link #BLOCKED}<br>一个线程因为等待临界区的锁被阻塞产生的状态
11      *     A thread that is blocked waiting for a monitor lock
12      *     is in this state.
13      *     </li>
14      * <li>{@link #WAITING}<br>一个无限期等待另一个线程执行特定操作的线程处于这种状态。
15      *     A thread that is waiting indefinitely for another thread to
16      *     perform a particular action is in this state.
17      *     </li>
18      * <li>{@link #TIMED_WAITING}<br>在指定等待时间内等待另一个线程执行某个操作的线程处于这种状态
19      *     A thread that is waiting for another thread to perform an action
20      *     for up to a specified waiting time is in this state.
21      *     </li>
22      * <li>{@link #TERMINATED}<br>线程运行结束处于这种状态
23      *     A thread that has exited is in this state.
24      *     </li>
25      * </ul>
26      *
27      * <p>
28      * A thread can be in only one state at a given point in time.
29      * These states are virtual machine states which do not reflect
30      * any operating system thread states.
31      *
32      * @since   1.5
33      * @see #getState
34      */
35     public enum State {
36         /**
37          * Thread state for a thread which has not yet started.
38          */
39         NEW,
40 
41         /**
42          * Thread state for a runnable thread.  A thread in the runnable
43          * state is executing in the Java virtual machine but it may
44          * be waiting for other resources from the operating system
45          * such as processor.
46          */
47         RUNNABLE,
48 
49         /**
50          * Thread state for a thread blocked waiting for a monitor lock.
51          * A thread in the blocked state is waiting for a monitor lock
52          * to enter a synchronized block/method or
53          * reenter a synchronized block/method after calling
54          * {@link Object#wait() Object.wait}.
55          */
56         BLOCKED,
57 
58         /**
59          * Thread state for a waiting thread.
60          * A thread is in the waiting state due to calling one of the
61          * following methods:
62          * <ul>
63          *   <li>{@link Object#wait() Object.wait} with no timeout</li>
64          *   <li>{@link #join() Thread.join} with no timeout</li>
65          *   <li>{@link LockSupport#park() LockSupport.park}</li>
66          * </ul>
67          *
68          * <p>A thread in the waiting state is waiting for another thread to
69          * perform a particular action.
70          *
71          * For example, a thread that has called <tt>Object.wait()</tt>
72          * on an object is waiting for another thread to call
73          * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
74          * that object. A thread that has called <tt>Thread.join()</tt>
75          * is waiting for a specified thread to terminate.
76          */
77         WAITING,
78 
79         /**
80          * Thread state for a waiting thread with a specified waiting time.
81          * A thread is in the timed waiting state due to calling one of
82          * the following methods with a specified positive waiting time:
83          * <ul>
84          *   <li>{@link #sleep Thread.sleep}</li>
85          *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
86          *   <li>{@link #join(long) Thread.join} with timeout</li>
87          *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
88          *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
89          * </ul>
90          */
91         TIMED_WAITING,
92 
93         /**
94          * Thread state for a terminated thread.
95          * The thread has completed execution.
96          */
97         TERMINATED;
98     }

 

posted @ 2020-04-18 21:21  奔波儿灞儿波奔  阅读(486)  评论(0)    收藏  举报