线程的6种状态
从Java源码(java.lang.Thread)中我们可以得知Java线程有6种状态。
java.lang.Thread中线程状态源码:
/**
* A thread state. A thread can be in one of the following states:
* <ul>
* <li>{@link #NEW}<br>
* A thread that has not yet started is in this state.
* </li>
* <li>{@link #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
* </li>
* <li>{@link #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
* </li>
* <li>{@link #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
* </li>
* <li>{@link #TIMED_WAITING}<br>
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
* </li>
* <li>{@link #TERMINATED}<br>
* A thread that has exited is in this state.
* </li>
* </ul>
*
* <p>
* A thread can be in only one state at a given point in time.
* These states are virtual machine states which do not reflect
* any operating system thread states.
*
* @since 1.5
* @see #getState
*/
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;
}
NEW(初始):新创建了一个线程对象,但还没有调用start()方法。
RUNNABLE(可运行):Java线程中将就绪(ready)和运行中(running)两种状态统称为“可运行”。线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。
BLOCKED(阻塞):表示线程阻塞于锁
WAITING(等待):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)。
TIMED_WAITING(超时等待):该状态不同于WAITING,它可以在指定的时间后自行返回。
TERMINATED(终止):表示该线程已经执行完毕。
线程状态切换
相关方法解析:
Thread.sleep(long milis),当前线程调用此方法后,当前线程进入TIMED_WAITING状态,但不会释放锁资源,milis时间后线程自动苏醒进入就绪状态。Thread.sleep()是给其它线程执行机会的最佳方式。
Thread.yield(),当前线程调用此方法后,当前线程放弃获取的CPU时间片,但不会释放锁资源,由运行状态变为就绪状态,让线程调度器再次选择线程。Thread.yield()方法让相同优先级的线程轮流执行,但不保证一定会轮流执行。世纪钟无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度器再次选中。Thread.yield()不会导致阻塞,该方法与Thread.sleep()类似,只是不能由用户指定暂停多长时间。
Thread.join()/Thread.join(long milis),当前线程里调用其它线程t的join方法,当前线程进入WAITING或者TIMED_WAITING状态,当前线程不会释放已经持有的对象锁。线程t执行完毕或者指定milis时间后,当前线程一般情况下会进入RUNNABLE状态,也有可能进入BLOCKED状态(因为join是基于Thread.wait()方法实现的)。
Object.wait()/Object.wait(long time),当前线程调用对象的wait()方法后,当前线程释放对象锁,进入等待队列。依靠Object.notify()或者Object.notifyAll()唤醒或者指定time时间后自动唤醒。
Object.notify()唤醒在此对象监视器上等待的单个线程,选择时任务性的。Object.notifyAll唤醒在此对象监视器上等待的所有线程。
LockSupport.park()/LockSupport.parkNanos(long nanos)/LockSupport.parkUntil(long deadlines),当执上述相关某一方法后,当前线程进入WAITING/TIMED_WAITING状态。对别Object.wait()方法,不需要获得锁就可以让线程进入WAITING/TIMED_WAITING状态,之后需要通过LockSupport.unpark(Thread thread)方法唤醒线程。