1. java线程状态转换
1.1. java线程的6种状态
在java.lang.Thread中定义了线程的6种状态:NEW
,RUNNABLE
,WAITING
,TIMED_WAITING
,BLOCKED
,TERMINATED
。
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.2. 线程状态的转换
1.2.1. NEW———>RUNNABLE
线程调用start()
1.2.2. RUNNABLE<———>WAITING
- wait-notify
- 线程获得对象锁,调用
obj.wait()
,从runnable变为waiting。 - 线程被
notify()
或者notifyAll()
或者interrupt()
:- 线程竞争到对象锁,从waiting变为runnable
- 否则,从waiting变为blocked
- 线程获得对象锁,调用
- join
- 当前线程调用
t.join()
,当前线程从runnable变为waiting- 线程t运行结束或者当前线程被
interrupt()
,当前线程从waiting变为runnable
- 线程t运行结束或者当前线程被
- 当前线程调用
- park-unpark
- 当前线程调用
LockSupport.park()
,从runnable变为waiting- 线程调用
LockSupport.unpark(t)
,线程t从waiting变为runnable - 线程调用
t.interrupt()
,线程t从waiting变为runnable
- 线程调用
- 当前线程调用
1.2.3. RUNNABLE<———>TIMED_WAITING
- wait-notify
- 线程获得对象锁,调用
obj.wait(long n)
,从runnable变为timed_waiting。 - 线程等待时间超过n毫秒或者被
notify()
或者notifyAll()
或者interrupt()
:- 线程竞争到对象锁,从timed_waiting变为runnable
- 否则,从timed_waiting变为blocked
- 线程获得对象锁,调用
- join
- 当前线程调用
t.join(long n)
,当前线程从runnable变为timed_waiting - 线程等待时间超过n毫秒或者线程t运行结束或者当前线程被
interrupt()
,当前线程从timed_waiting变为runnable
- 当前线程调用
- park-unpark
- 当前线程调用
LockSupport.parkNanos(long nanos)
或LockSupport.parkUntil(long millis)
,从runnable变为timed_waiting - 线程调用
LockSupport.unpark(t)
,线程t从timed_waiting变为runnable - 线程调用
t.interrupt()
,线程t从timed_waiting变为runnable
- 当前线程调用
1.2.4. RUNNABLE<———>BLOCKED
- 线程进入
synchronized
代码块之前没有竞争到锁,从runnable变为blocked - 等待锁的线程重新竞争到锁,从blocked变为runnable
1.2.5. RUNNABLE———>TERMINATED
线程执行完毕