面试题92:Java线程有几种状态?

2023-05-10 10:54:00 浏览数 (2)

  • Java线程一共分为6种状态,分别为:
    • 新建状态(NEW)
    • 就绪状态(RUNNABLE)
    • 阻塞状态(BLOCKED)
    • 等待状态(WAITING)
    • 限时等待状态(TIMED_WAITING)
    • 结束状态(TERMINATED)
  • 我们也可以通过Tread中的State枚举查看这六种状态的定义:
代码语言:javascript复制
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状态 表示线程所需的一切资源都已经准备好了。处于就绪状态的线程,只是说明此线程已经做好了被运行的准备,随时等待CPU调度执行,而并不是说调用了start()方法,这个线程就会被立即运行。
  • BLOCKED状态 如果线程在执行过程中遇到了synchronized或者Lock,那么就会进入到阻塞状态。处于阻塞状态的线程会放弃对CPU的使用权,停止执行,直到次线程进入到了就绪的状态,才会有机会再次被CPU调用运行。
  • WAITING状态 如果线程被调用了没有超时时间的wait方法,那么会进入无限等待状态。直到被notify通知才可以继续执行。
  • TIMED_WAITING状态 与WAITING状态相似,区别是,在有限的时间内进行线程等待。

0 人点赞