您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 2浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java线程状态

小志的博客 发布时间:2021-11-17 20:00:00 ,浏览量:2

目录
    • 1、Java线程6中状态
    • 2、Tread类中State枚举定义的6种状态
    • 3、各种状态的图解
    • 4、各种状态的解释

1、Java线程6中状态

Java线程一共分为6种状态,如下所示

  • NEW:新建状态
  • RUNNABLE:就绪状态
  • BLOCKED:阻塞状态
  • WAITING:等待状态
  • TIMED_WAITING:限时等待状态
  • TERMINATED:结束状态
2、Tread类中State枚举定义的6种状态

通过Tread中的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:
         * 
    *
  • {@link Object#wait() Object.wait} with no timeout
  • *
  • {@link #join() Thread.join} with no timeout
  • *
  • {@link LockSupport#park() LockSupport.park}
  • *
* *

A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called Object.wait() * on an object is waiting for another thread to call * Object.notify() or Object.notifyAll() on * that object. A thread that has called Thread.join() * 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: *

    *
  • {@link #sleep Thread.sleep}
  • *
  • {@link Object#wait(long) Object.wait} with timeout
  • *
  • {@link #join(long) Thread.join} with timeout
  • *
  • {@link LockSupport#parkNanos LockSupport.parkNanos}
  • *
  • {@link LockSupport#parkUntil LockSupport.parkUntil}
  • *
*/ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
3、各种状态的图解

各种状态图解,如下图所示:

在这里插入图片描述

4、各种状态的解释
  • NEW状态 表示刚刚创建的线程,这种线程还没开始执行。等到线程的start()方法调用时,才表示线程开始执行。

  • RUNNABLE状态 表示线程所需的一切资源都已经准备好了。处于就绪状态的线程,只是说明此线程已经做好了被运行的准备,随时等待CPU调度执行,而并不是说调用了start()方法,这个线程就会被立即运行。

  • BLOCKED状态 如果线程在执行过程中遇到了synchronized或者Lock,那么就会进入到阻塞状态。处于阻塞状态的线程会放弃对CPU的使用权,停止执行,直到次线程进入到了就绪的状态,才会有机会再次被CPU调用运行。

  • WAITING状态 如果线程被调用了没有超时时间的wait方法,那么会进入无限等待状态。直到被notify通知才可以继续执行。

  • TIMED_WAITING状态 与WAITING状态相似,区别是,在有限的时间内进行线程等待。

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.0526s