状态流转图
能够被中断的阻塞称为轻量级阻塞,对应的线程状态是WAITING或者TIMED_WAITING;而像 synchronized 这种不能被中断的阻塞称为重量级阻塞,对应的状态是 BLOCKED。如图所示:调用不同 的方法后,一个线程的状态迁移过程。
public enum State {/*** Thread state for a thread which has not yet started.*/NEW,/*** Thread state for a runnable thread. A thread in the runnable* state is executing in the Java virtual machine but it may* be waiting for other resources from the operating system* such as processor.*/RUNNABLE,/*** Thread state for a thread blocked waiting for a monitor lock.* A thread in the blocked state is waiting for a monitor lock* to enter a synchronized block/method or* reenter a synchronized block/method after calling* {@link Object#wait() Object.wait}.*/BLOCKED,/*** Thread state for a waiting thread.* A thread is in the waiting state due to calling one of the* following methods:* <ul>* <li>{@link Object#wait() Object.wait} with no timeout</li>* <li>{@link #join() Thread.join} with no timeout</li>* <li>{@link LockSupport#park() LockSupport.park}</li>* </ul>** <p>A thread in the waiting state is waiting for another thread to* perform a particular action.** For example, a thread that has called <tt>Object.wait()</tt>* on an object is waiting for another thread to call* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on* that object. A thread that has called <tt>Thread.join()</tt>* is waiting for a specified thread to terminate.*/WAITING,/*** Thread state for a waiting thread with a specified waiting time.* A thread is in the timed waiting state due to calling one of* the following methods with a specified positive waiting time:* <ul>* <li>{@link #sleep Thread.sleep}</li>* <li>{@link Object#wait(long) Object.wait} with timeout</li>* <li>{@link #join(long) Thread.join} with timeout</li>* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>* </ul>*/TIMED_WAITING,/*** Thread state for a terminated thread.* The thread has completed execution.*/TERMINATED;}
1、new(新创建)
new 表示线程被创建但尚未启动的状态,当我们用new Thread()新创建一个线程时 如果线程没有开始运行 start() 方法,所以也没有开始开始执行run()方法里面的代码,那么此时他的状态就是new,而一旦线程调用了start(),他的状态就从new 变成了Runnable
2、Runnable(可运行)
java 中的Runnable 状态对应操作系统的running和ready
就是 有可能正在被执行,也可能没有正在执行,正在等待被分配的cpu资源
3、Blocked(被阻塞)
从Running状态进入blocked状态只有一种可能,就是进入synchronized保护的代码时没有抢到monitor 锁
无论是进入synchronized代码块还是 synchronized方法,都是一样的
4、Waiting(等待)
1、没有设置Timeout参数的Object.wait()方法
2、没有设置Timeout参数的Thread.join()方法
3、LockSupport.park()方法
5、Time_waiting(计时等待)
1、设置了时间参数的Thread.sleep(Long millis)方法
2、设置了时间参数的Object.wait(long timeout)方法
3、设置了时间参数的LockSupport.parkNanos(long nanos) 方法和 LockSupport.partUntil(long deadline)方法
6、Terminated(被终止)
进入Terminated有2中可能
1、run() 方法执行结束,线程正常退出
2、出现一个没有捕获的异常,终止了run()方法,最终导致意外终止
阻塞状态
blocked
从 blocked状态进入到runnable状态要求线程获取monitor锁
waiting
从waiting状态流转到其他状态则比较特殊
首先,该状态是不现实的,因为无论经过多久,都不会恢复
只有LockSupport.unpark()或join的线程运行结束/被中断
如果其他线程调用notify()或者notifyAll()来唤醒它,它会直接进入到Bolcked状态
因为 因为唤醒waiting线程的线程 如果调用notify()或者notifyAll(),要求必须首先持有该monitor锁,所以处于waiting状态的线程唤醒时拿不到该锁,就会进入到Bolcked状态
time_witing
Time waiting中执行notify或者notifyAll也是一样道理
如果 它的超时时间到了且能直接获取到锁/join的线程结束/被中断/调用了LockSupport.unpark(),会直接恢复到Runnable状态,而不需要经历Block 状态

