Java线程的几种状态

java.lang.Thread.State中定义的集中Java线程的状态:

 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> 
 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      * 没有start()的线程状态 
38      */  
39     NEW,  
40    
41     /** 
42      * 可运行线程的线程状态。处于可运行状态的线程正在Java虚拟机中执行,但它可能正在等待来自操作系统(如处理器)的其他资源 
43      */  
44     RUNNABLE,  
45    
46     /** 
47      * 线程处于阻塞状态。在进入或者重新进入synchronized代码块/方法时,等待monitor lock的一种状态 
48      */  
49     BLOCKED,  
50    
51     /** 
52      * 线程处于等待状态。,由于调用以下方法之一,线程会处于等待状态: 
53      *    Object.wait()  没有超时时间 
54      *    Thread.join()  没有超时时间 
55      *    LockSupport.park() 
56      */  
57     WAITING,  
58    
59     /** 
60      * 具有指定等待时间的等待状态。调用以下方法之一,在指定的等待时间内,使线程处于等待状态: 
61      *   Thread.sleep 
62      *   Object#wait(long)  有超时时间 
63      *   Thread.join(long)  有超时时间 
64      *   LockSupport.parkNanos 
65      *   LockSupport.parkUntil 
66      */  
67     TIMED_WAITING,  
68    
69     /** 
70      * 终止状态。 线程已完成执行 
71      */  
72     TERMINATED;  
73 }  

 

上述Java代码定义的几个状态中其实是没有running状态的。

线程的runnable状态是从虚拟机的角度来看的,表示这个线程正在运行。 但是处于Runnable状态的线程不一定真地消耗CPU. 处于Runnable的线程只能说明该线程没有阻塞在java的wait或者sleep方法上, 同时也没等待在锁上面。 但是如果该线程调用了本地方法, 而本地方法处于等待状态, 这个时候虚拟机是不知道本地代码中发生了什么, 此时尽管当前线程实际上也是阻塞的状态, 但实际上显示出来的还是runnable状态,这种情况下是不消耗CPU的。

 

阻塞与等待的区别:

 

阻塞:当一个线程试图获取对象锁(非java.util.concurrent库中的锁,即synchronized),而该锁被其他线程持有,则该线程进入阻塞状态。它的特点是使用简单,由JVM调度器来决定唤醒自己,而不需要由另一个线程来显式唤醒自己,不响应中断。


等待:当一个线程等待另一个线程通知调度器一个条件时,该线程进入等待状态。它的特点是需要等待另一个线程显式地唤醒自己,实现灵活,语义更丰富,可响应中断。例如调用:Object.wait()、Thread.join()以及等待Lock或Condition。

 

 

参考文章:

线程状态

posted @ 2018-03-20 17:36  欠扁的小篮子  阅读(1371)  评论(0编辑  收藏  举报