Java教程

Java线程的生命周期

本文主要是介绍Java线程的生命周期,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java线程的生命周期

​ 在Thread类中,我们可以看到有个内部枚举类State:

/**
 * A thread state. A thread can be in one of the following states:
 * 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.
 * BLOCKED A thread that is blocked waiting for a monitor lock is in this state.
 * WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
 * 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.
 * TERMINATED A thread that has exited is in this state.
 * A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.
 */
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.
     * <p>
     * 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;
}

​ Java线程共有六种状态,并且线程在同一时间只能处于一种状态。这些状态都是虚拟机状态,并不反应任何操作系统的线程状态:

  • ​ NEW

  • ​ RUNNABLE

  • ​ BLOCKED

  • ​ WAITING

  • ​ TIMED_WAITING

  • ​ TERMINATED

    翻译一下源码中的注释,我们可以用下面这张图表示出线程状态转换关系:

在这里插入图片描述

那new->runnable->terminated这三种状态能不能回退呢?我们写个demo测试下:

  @Test
    public void thread1() throws InterruptedException {
        Thread thread =
                new Thread(() -> System.out.println("hello,我是子线程,我的name是:" + Thread.currentThread().getName()));
        thread.start();
        thread.start();
        System.out.println(thread.getState());
        thread.join();
        System.out.println("hello,我是主线程,我的name是:" + Thread.currentThread().getName());
    }

可以看到,不出意外的抛了异常:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ib54rshw-1636863700194)(image-20211112174746368.png)]

我们去瞅一眼start方法:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b3cWRFW7-1636863700197)(image-20211112174814298.png)]

​ 可以看到,start方法上来就对线程的状态进行了判断,只有status=0,也就是刚创建出来的线程才可以。那么我们就可以得出结论new->runnable->terminated这三种状态是不可逆的。


这篇关于Java线程的生命周期的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!