Java线程状态切换详解

一、线程状态说明

java.lang.Thread类中定义了线程状态枚举java.lang.Thread.State,以下为各状态说明。

1、NEW(新建)

NEW是线程调用new()创建后且未调用start()启动时的状态。

2、RUNNABLE(可运行)

RUNNABLE包含Ready(就绪)和Running(运行中)。

就绪状态的线程在系统调度分配时间片后进入运行中。

Thread.yield()调用后线程从运行中进入到就绪状态,但是不会使用同步。

3、BLOCKED(阻塞)

BLOCKED是线程等待获取监视器锁去进入(或重新进入)同步代码块或者同步方法。

4、WAITING(等待)

当调用Obeject.wait()、Thread.join()、LockSupport.park()方法后线程进入WAITING。

WAITING是线程无限期等待,等待其他线程执行特定操作:

(1)、当线程调用Object.wait()后,需要等待其他线程调用Object.notify()或者Object.notifyAll()唤醒;

(2)、当其他线程调用Thread.join()后,需要等他其他线程执行结束后唤醒;

5、TIMED_WAITING(超时等待)

TIMED_WAITING是指线程等待特定时间,超时后进入RUNNABLE状态。

调用以下方法会使线程进入TIMED_WAITING:

(1)、Thread.sleep(long millis);

线程sleep,不会释放同步锁。

(2)、Object.wait(long timeout);

等待Object.notify()或者Object.notifyAll()后或者超时后继续执行;

必须在同步块和同步方法中使用,调用前必须持有锁,调用后会释放同步锁。

(3)、thread.join(long millis);

等待thread线程执行结束后活超时后继续执行。

实际使用wait()实现。

(4)、LockSupport.parkNanos(long nanos);

在阻塞调用线程nanos纳秒后继续执行,无需持有锁。

(5)、LockSupport.parkUntil(long deadline);

阻塞当前线程至deadline时间戳后或LockSupport.unpack(thread)继续执行,无需持有锁。deadline:1970.1.1起的毫秒数。

6、TERMINATED(结束)

TERMINATED是指线程完成执行。

二、线程状态切换

图片链接:https://www.processon.com/view/link/5b459aa9e4b054aa54b124b2

后面将根据jdk源码了解这些操作线程的具体方法的实现,下文链接:

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;
    }

 

参考:

https://www.cnblogs.com/trust-freedom/p/6606594.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值