Java中线程的生命周期

大家好,我是joker,希望你快乐。

首先说一道常见的面试题,一个线程能不能两次调用start()方法,会有什么效果?

正如大家知道的,答案:不能,线程启动后不能再次启动,会报运行时异常,IllegalThreadStateException

线程是什么?

从操作系统的角度,可以简单的认为,线程是系统调度的最小单元,一个进程可以包含多个线程,作为任务的真正运作者,有自己的栈(stack),寄存器(register),本地存储(thread local)等,但是会和进程内其他线程共享文件描述符,虚拟地址空间。

线程分内核线程,用户线程,Java的线程实现是和虚拟机相关的。Hotspot线程模型是一对一映射到操作系统的内核线程。

守护线程(Deamon Thread):后台线程,不希望其影响应用退出,如果JVM发现只有守护线程,将结束进程。

JDK5以后Java Thread Stack默认为1M

线程各状态说明

接下来线程启动后都有哪些状态?这些状态如何相互转换?

线程生命周期不同状态:可以在java.lang.Thread.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):线程已经在JVM中执行,可能正在运行,也可能正在就绪队列中等待系统分配CPU时间片

阻塞(BLOCKED):同步相关,表示线程正在等待Monitor Lock

等待(WAITING):正在等待其他线程采取某些操作

计时等待(TIMED_WAIT):进入条件和等待状态类似,但是调用的是存在超时条件的方法

终止(TERMINATED):线程终止运行,不管是正常执行结束还是意外退出

各个状态之间的转化:

查看JVM运行线程的方式

通过线程组,一直找到最顶级的父线程组

通过jstack

通过MXBean

posted @ 2023-12-21 23:24  Crazy_Joker  阅读(6)  评论(0编辑  收藏  举报