博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程的几个状态转换
阅读量:5051 次
发布时间:2019-06-12

本文共 4525 字,大约阅读时间需要 15 分钟。

java线程的几种状态转化图:

 

 

Java线程一共有七个状态,分别是新建(New),就绪(Ready to run),运行中(Running),睡眠(Sleeping),阻塞(Blocked),等待(Waiting),死亡(Dead/Terminate)

线程间的状态转化

1、新建(new)

新建一个线程,还未调用start方法

Thread t = new Thread();

2、就绪(runnable)

线程对象创建后,其他线程调用了该对象的start方法后,该状态的线程位于可运行线程池中,等待被线程调度选中,获取cpu的使用权

3、运行中(running)

可运行状态(Runnable)线程获得了cpu时间片(timeslice),执行程序代码

4、睡眠(sleeping)

也称TIME_WAITING(有等待时间的等待状态)

线程主动调用这几个方法:

1)Thread.sleep方法

2)object的wait方法 带有时间

3)Thread.join方法,带有时间

4)LockSupport的parkNanos方法,带有时间

5、阻塞(blocked)

阻塞状态是线程因为某种原因放弃了cpu使用权,暂时停止运行。直到线程进入可运行状态,才有机会再次获得cpu timeslice转到运行状态,阻塞的情况有两种:

1)同步阻塞:运行的线程进入了一个sychronized方法,若该同步锁被别的线程占用,则jvm会把该线程放入锁池中

2)其他阻塞:运行的线程发出了IO请求时,JVM会把该线程置为阻塞状态,当IO处理完毕后,线程重新转入可运行状态

6、等待(waiting)

运行中的线程执行了4个方法中的任意方法:

1)Object的wait方法,并没有设置时间

2)Thread的join方法,并没有设置时间

3)Locksupport的park方法

4)Condition的wait方法

7、死亡(dead)

线程run方法、main方法执行结束,或者因为异常退出了run方法,则该线程结束生命周期。死亡线程不可复生。

/**     * A thread state.  A thread can be in one of the following states:      * 
    *
  • {
    @link #NEW}
    * A thread that has not yet started is in this state. *
  • *
  • {
    @link #RUNNABLE}
    * A thread executing in the Java virtual machine is in this state. *
  • *
  • {
    @link #BLOCKED}
    * A thread that is blocked waiting for a monitor lock * is in this state. *
  • *
  • {
    @link #WAITING}
    * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. *
  • *
  • {
    @link #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. *
  • *
  • {
    @link #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. * * @since 1.5 * @see #getState */ 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; }

 

转载于:https://www.cnblogs.com/cherish010/p/8610955.html

你可能感兴趣的文章
代码整洁
查看>>
蓝桥杯-分小组-java
查看>>
Java基础--面向对象编程1(类与对象)
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
iOS-内存管理
查看>>
docker固定IP地址重启不变
查看>>
hadoop的wordcount程序
查看>>
冲刺二阶段-个人总结07
查看>>
C语言 基础练习40题
查看>>
[Swift]LeetCode128. 最长连续序列 | Longest Consecutive Sequence
查看>>
[Swift通天遁地]一、超级工具-(9)在地图视图MKMapView中添加支持交互动作的标注图标...
查看>>
js版base64()
查看>>
poj3006---素数筛法
查看>>
c语言结构体排序示例
查看>>
openresty nginx systemtap netdata
查看>>
[Angular] Make a chatbot with DialogFlow
查看>>
javascript坐标:event.x、event.clientX、event.offsetX、event.screenX 用法
查看>>
genymotion不能启动模拟器的处理姿势
查看>>
vs2005下使用sql 2000或其他数据库作为membership的默认提供程序
查看>>