Java线程状态

isen
isen
发布于 2023-09-23 / 52 阅读 / 0 评论 / 0 点赞

Java线程状态

一、线程状态

public class Thread implements Runnable {

    /**

    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.

    */

    //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

线程在虚拟机(JVM)上处于执行状态,但是它可能需要等待系统资源,比如处理器。即可能在处理器上并未真正执行,包括操作系统线程状态中的Running 和 Ready

BLOCKED

线程处于阻塞状态,由于synchronized或者wait引起等待获取对象锁

WAITING

线程处于无限等待状态,由于调用了wait无限等待、join无限等待、LockSupport.park无限等待,等待其他线程调用特定的方法,比如notify、notifyAll或者其他线程结束(join引起时)

TIMED_WAITING

线程处于有限等待状态,由于调用了sleep、wait有限等待、join有限等待、LockSupport.parkNanos有限等待、LockSupport.parkUntil有限等待,等待其他线程调用特定的方法或者其他线程结束或者超时

TERMINATED

线程终结状态,线程异常退出或者正常执行结束

二、状态图

这个状态图不准确,

* LockSupport#park,在unpark、超时、interrupt之后都会进入RUNNABLE,即不会进入BLOCKED状态

* sleep超时之后,也是进入RUNNABLE

* wait、join会释放锁,所以需要重新获取锁

上述图中的方法分别属于:

Object(需要加锁、解锁)

wait、notify、notifyAll

Thread

start、sleep、join、interrupt

LockSupport

pack、parkNanos、parkUntil

关键字synchronized

三、特别注意

当其他线程调用notify或者notifyAll时,当前处于WAITING或者TIMED_WAITING的线程将会转为BLOCKED状态

yield()方法时让出当前cpu,线程依旧处于RUNNABLE

四、notify和notifyAll异同

下面等待状态默认包括有限、无限等待

同:

都是用于通知其他处于等待状态的线程

异:

notify选择一个等待线程进行通知,同时也只有被选中的等待线程退出等待(wait)状态,进入阻塞状态,等待获取对象锁。当该被唤醒且获取了对象锁的线程运行结束后,即使对象锁是空闲状态,其他等待线程也无法获取锁,因为还未退出等待(wait)状态。

notifyAll通知所有等待该对象锁的线程,让他们全部退出等待(wait)状态,进入阻塞状态然后他们会竞争对象锁,以后即使没有notify或者notifyAll,也会竞争锁。