Loading

【线程基础】线程的生命周期

线程的六大状态

在多线程从创建到销毁,可能会经历六种状态
这六种状态我们可以看一下Java多线程的源码中是怎么解释的

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虚拟机中执行,但它可能正在等待来自操作系统(如处理器)的其他资源。也就是调用了start方法之后正在运行的状态。但这里需要注意下的是当调用start方法之后,不一定会立即改变状态,线程可能还在做一下准备工作,这个时候的状态是不确定的。

BLOCKED(被挂起状态)

等待监视器锁定时阻塞的线程的线程状态。处于阻塞状态的线程正在等待监视器锁进入 同步块/方法 或在调用后重新输入 同步块/方法

WAITING(无条件等待状态)

一个线程进入 WAITING 状态是因为调用了以下方法:

  • 不带时限的 Object.wait 方法
  • 不带时限的 Thread.join 方法
  • LockSupport.park
    然后会等其它线程执行一个特别的动作,比如:
  • 一个调用了某个对象的 Object.wait 方法的线程会等待另一个线程调用此对象的 Object.notify() 或 Object.notifyAll()。
  • 一个调用了 Thread.join 方法的线程会等待指定的线程结束。

TIMED_WAITING(有条件等待状态)

具有指定等待时间的等待线程的线程状态。由于使用指定的正等待时间调用以下方法之一,线程处于定时等待状态。也就是当线程调用有定好时间的sleep(时间),wait(时间),join(时间),LockSupport.parkNanos(时间),LockSupport.parkUntil(时间)的方法之后的状态。在指定的时间没有被唤醒或者等待线程没有结束,会被系统自动唤醒。

TERMINATED(结束状态)

已终止的线程状态线程。那个线程已完成执行。也就是线程中的run方法已经被执行完成。

状态间的转化图示

通过上面我们知道了线程生命周期中拥有六种状态,并且还知道了线程状态的含义。在线程的生命周期中,同一时刻只有一种状态,通过线程的getState方法可以获取线程的状态。

转换图如下:

未命名文件.png

阻塞状态

如果仔细理解了上面的文章后,我们会想到一个问题,BLOCKED(被挂起状态),WAITING(无条件等待状态),TIMED_WAITING(有条件状态)这三个状态貌似都是一样的,那么他们有什么区别呢?

其实我们一般习惯而言把这三种状态都可以理解为阻塞状态
我们对阻塞状态有以下几种分类

  • 等待阻塞

等待阻塞也就是Object.wait(),当线程运行该方法时,JVM就会把该线程放入到等待队列(waitting queue)中

  • 同步阻塞

同步阻塞也就是Blocked状态,运行的线程在获取对象的同步锁的时候,若该同步锁被别的线程占用,则JVM就会把该线程放入到锁池(lock pool)中

  • 其他阻塞

线程执行Thread.sleep(time),或者Thread.join()方法时,或者是发出了I/O请求时,JVM就会把该线程变为阻塞状态

当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入可运行(runnable)状态

线程在阻塞等待monitor lock(监视器锁)

一个线程在进入synchronized修饰的临界区的时候,或者在synchronized临界区中调用Object.wait然后被唤醒重新进入synchronized临界区都对应该态。

可以参考下面这张图的理解
20200817092726889.png

posted @ 2022-01-24 19:39  邓小白  阅读(49)  评论(0编辑  收藏  举报