博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java线程的状态及切换
阅读量:5861 次
发布时间:2019-06-19

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

线程状态

(1)NEW(新建尚未运行/启动)

还没调用start,或者调用了start()方法,不一定立即改变线程状态,中间可能需要一些步骤才完成一个线程的启动。

(2)RUNNABLE(处于可运行状态:正在运行或准备运行)

start调用结束,线程由NEW变成RUNNABLE,存活着,并尝试占用CPU资源,yield操作时,线程还是Runnable状态,只是它有一个细节的内部变化,做一个简单的让步。在Java层面是Runnable的状态,并不代表一定处于运行中的状态,比如BIO中,线程正阻塞在网络等待的时候,看到的状态依然是Runnable状态,而底层线程已经被阻塞住了。

(3)BLOCKED(等待获取锁时进入的状态)

线程被挂起了,原因通常是因为它在等待一个锁,当某个synchronized正好有线程在使用时,一个线程尝试进入这个临界区,就会被阻塞,直到另一个线程走完临界区或发生了相应锁对象的wait操作后,它才有机会去争夺进入临界区的权利。当抢到锁之后,才会从blocked状态恢复到runnable状态。这个状态它好像什么也不做一样。

(4)WAITING(通过wait方法进入的等待)

当wait,join,park方法调用时,进入waiting状态。前提是这个线程已经拥有锁了。

blocked和waiting状态的区别是:

A、blocked是虚拟机认为程序还不能进入某个区域,因为同时进去就会有问题,这是一块临界区。
B、发生wait等操作的先决条件是要进入临界区,也就是线程已经拿到锁了,自己可能进去做了一些事情,但此时通过判定业务上的参数,发现还有一些其他配合的资源没有准备充分,那么自己就等等再做其他事情。

在waiting状态下,如果发生了interrupt操作,则处于该状态的线程在内部会抛出一个InterruptedException,这个异常应当在run方法内捕获,使得run方法正常地执行完成,当然捕获异常后,是决定让线程继续运行,还是结束等要根据业务场景才处理。

如果发生了notify动作,则会从等待池当中唤醒一个线程重新恢复到Runnable状态,如果是notifyall操作,则唤醒所有等待线程。

(5)TIMED_WAITING(通过sleep或wait timeout方法进入的限期等待的状态)

通过wait(t),sleep(t),join(t),parkNanos,parkUntil等方法进入此状态。当时间达到时触发线程回到工作状态Runnable。

interrupt只对处于waiting或timed_waiting状态的线程起作用,对其他状态不起作用。

(6)TERMINATED(线程终止状态)

线程结束了,就处于这种状态,也就是run方法运行完了。这只是Java语言级别的一种状态,在操作系统内部可能已经注销了相应的线程,或者将它复用给其他需要使用线程的请求。

线程状态切换

clipboard.png

详细图

clipboard.png

线程状态源码

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

转载地址:http://dfgjx.baihongyu.com/

你可能感兴趣的文章
社会化评论插件之友言
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
ssl协议工作原理
查看>>
水仙花代码
查看>>
XenDesktop 5 MCS Pool/池类型桌面重启/注销即还原
查看>>
vsftpd 配置:chroot_local_user与chroot_list_enable详解
查看>>
solidworks中 toolbox调用出现未配置的解决方法
查看>>
CloudStack 4.3功能前瞻
查看>>
洛克菲勒家族是如何发家的,我们都看看
查看>>
2018.1.3 4周2次课
查看>>
Hadoop系列之三:函数式编程语言和MapReduce
查看>>
网站 学习资源站 Centos linux
查看>>
linux下修改root密码以及找回密码的方法
查看>>
Kickstart无人值守批量安装linux系统
查看>>
什么是自定义解析?
查看>>
Alpha 冲刺报告(2/10)
查看>>
ultraedit配置
查看>>
我的友情链接
查看>>
jvm内存快照dump文件太大,怎么分析
查看>>