天天看点

Thread线程有哪几种状态,这篇文章带你深入了解!

Thread线程有哪几种状态,这也是面试中很多小伙伴都会被问到的知识点。Thread线程状态是应该掌握的基础知识。但是很多小伙伴如果没有总结过的话,面试过程可能会懵圈,这里我稍作整理,希望小伙伴们可以查漏补缺。

Thread源码中有定义State的枚举类,State枚举类中有明确定义线程的状态。源码如下。

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、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED。

我们分别对线程不同状态进行简单描述:

  1. NEW:尚未启动的线程的线程状态。
  2. RUNNABLE:可运行线程的线程状态。线程正在运行或者正在获取CPU时间片。
  3. BLOCKED:线程的线程状态被阻止,正在等待监视器锁。
  4. WAITING:等待线程的线程状态。调用某个线程的wait()、join()、 locksupport park()处于等待状态的线程。
  5. TIMED_WAITING:具有指定等待时间的等待线程的线程状态。调用某个线程的具有指定正等待时间的方法sleep(long)、wait(long)、join(long)、LockSupport Parknanos()、LockSupport ParkUntil()处于指定等待时间的等待线程的线程状态。
  6. TERMINATED:终止线程的线程状态。

我这边也整理了一份相关的线程状态转换图以供大家参考。

Thread线程有哪几种状态,这篇文章带你深入了解!

以上代供参考,如有不当之处,欢迎指出!!!

更多干货,欢迎大家关注和联系我。期待和大家一起更好的交流、探讨技术!!!

Thread线程有哪几种状态,这篇文章带你深入了解!