在Java中java.lang.Thread类有一个变量threadStatus,标示了该线程的当前状态,它是一个int类型,但是对应的get方法返回值是一个枚举(Thread的内部类),源码如下:
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
Thread state for a thread which has not yet started.
尚未启动的线程的线程状态。表明线程尚未开始。
RUNNABLE
可运行线程。一个线程在Java虚拟机处于可运行状态,即它在等待其他资源或操作系统的处理,一旦获取到CPU资源进行了执行,则进入人们所说的隐式RUNNING状态(这个状态JVM并不关心,我们也不是特别关注,一般的JVM监控工具都不会统计这种状态)。
BLOCKED,
线程运行过程中,可能由于各种原因进入阻塞状态:
出于某种原因,比如等待用户输入等而让出当前的CPU给其他的线程执行时,会进入BLOCKED状态。
线程通过调用sleep方法进入睡眠状态
所谓阻塞状态是正在运行的线程没有运行结束,暂时让出CPU,这时其他处于就绪状态的线程就可以获得CPU时间,进入运行状态。
阻塞状态的线程正在等待一个监视锁
来进入一个同步块/方法或者在调用之后从新进入一个同步块/方法
WAITING,
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
一个线程正在无限期地等待另一个线程来唤醒是在这种状态下。
一个线程处于等待状态,因为调用一个线程以下方法:
Object.wait( with no timeout)
thread.join( with no timeout)
LockSupport.park( with no timeout)
一个线程处于WAITING状态需要由于另一个线程激活。例如,一个线程执行Object.wait()后会等待另一个线程调用对象Object.notify()或Object.notifyAll()。
一个线程调用了另一个线程的Thread.join()方法,则在另一个线程执行后才会继续执行(join方法可以指定延迟执行时间)。
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
一个线程在一个时间阈值内等待另一个线程来唤醒就处于这种状态,达到阈值则回到RUNNABLE状态。
具有指定等待时间的等待线程的线程状态。一个线程在等待状态时,由于调用下列方法具有指定的正等待时间:
- 调用Thread.sleep()
- 调用Object.wait()超过时间阈值
- 调用Thread.join()超过时间阈值
- 调用LockSupport.parkNanos(Object,long)
- 调用LockSupport.parkUntil(Object,long)
A thread that has exited is in this state.
一个线程退出就处于这种状态。
线程执行完成就会变为这个状态。
程执行完了或者因异常退出了run()方法,该线程结束生命周期。