Java千百问_01基本概念(007)_线程的状态有哪些

来源:互联网 发布:5ghz网络设置 编辑:程序博客网 时间:2024/05/22 17:36

点击进入_更多_Java千百问

1、线程的状态有哪些

在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;    }

可以看到,线程包含6个可见状态:NEWRUNNABLEBLOCKEDWAITINGTIMED_WAITINGTERMINATED
我们还可以通过JavaVisualVM的线程监控可以看到,它会监控线程的运行(对应RUNNABLE)、休眠(对应NEW)、等待(WAITING+TIMED_WAITING)、驻留(BLOCKED)、监视状态已完成(TERMINATED)六种状态。

了解监控JVM看这里:如何监控jvm的运行情况

对于State枚举,源码注释写的很清楚,这里我们具体看一下:

NEW
A thread that has not yet started is in this state.
一个还没有开始的线程就处于这种状态。

表明线程尚未开始

RUNNABLE
A thread executing in the Java virtual machine is in this state.
一个在JVM中执行的线程处于这种状态。

可运行线程。一个线程在Java虚拟机处于可运行状态,即它在等待其他资源或操作系统的处理,一旦获取到CPU资源进行了执行,则进入人们所说的子状态RUNNING状态(这个状态JVM并不关心,我们也不是特别关注,一般的JVM监控工具都不会统计这种状态)。

BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
一个被阻塞的线程就处于这种状态,它正在等待监视器锁
出于某种原因,比如等待用户输入等而让出当前的CPU给其他的线程执行时,会进入BLOCKED状态。
BLOCKED状态的线程会一直等待监视器锁,而后执行synchronized代码块/方法。或者在调用Object.wait()后,执行synchronized代码块/方法。

WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
一个线程正在无限期地等待另一个线程来唤醒是在这种状态下。

通过以下方法进入WAITING状态:

  1. 调用Object.wait()且没有超时
  2. 调用Thread.join()且没有超时
  3. 调用LockSupport.park(Object)

一个线程处于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状态

通过以下方法进入TIMED_WAITING状态:

  1. 调用Thread.sleep()
  2. 调用Object.wait()超过时间阈值
  3. 调用Thread.join()超过时间阈值
  4. 调用LockSupport.parkNanos(Object,long)
  5. 调用LockSupport.parkUntil(Object,long)

TERMINATED
A thread that has exited is in this state.
一个线程退出就处于这种状态。

线程执行完成就会变为这个状态。

3 1
原创粉丝点击