摘要:如果发生了动作,则会从等待池当中唤醒一个线程重新恢复到状态,如果是操作,则唤醒所有等待线程。当时间达到时触发线程回到工作状态。线程终止状态线程结束了,就处于这种状态,也就是方法运行完了。线程状态切换详细图线程状态源码
线程状态 (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语言级别的一种状态,在操作系统内部可能已经注销了相应的线程,或者将它复用给其他需要使用线程的请求。
线程状态切换详细图
线程状态源码</>复制代码
/**
* 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.ucloud.cn/yun/65814.html
摘要:上下文切换会影响到线程的执行速度,对于系统来说意味着会消耗大量的时间减少上下文切换的方式无锁并发编程,在多线程竞争锁时,会导致大量的上下文切换。线程在中的使用在中实现多线程的方式比较简单,因为中提供了非常方便的来实现多线程。 文章简介 上一篇文章我们了解了进程和线程的发展历史、线程的生命周期、线程的优势和使用场景,这一篇,我们从Java层面更进一步了解线程的使用 内容导航 并发编程的...
摘要:应用性能优化是一个程序员必须要考虑的问题,典型的性能问题如页面响应慢接口超时,服务器负载高并发数低,数据库频繁死锁等。诊断对于主要关注平均负载,使用率,上下文切换次数。应用诊断及工具应用代码性能问题是相对好解决的一类性能问题。 Java 应用性能优化是一个程序员必须要考虑的问题,典型的性能问题如页面响应慢、接口超时,服务器负载高、并发数低,数据库频繁死锁等。Java应用性能的瓶颈点非常...
摘要:多线程一线程模型实现线程有三种方式使用内核线程实现使用用户线程实现和使用用户线程加轻量级进程混合实现。这种轻量级进程与内核线程之间的关系称为一对一的线程模型。是通知所有等待对象控制权的线程继续运行。 Java多线程 一、Java线程模型 实现线程有三种方式:使用内核线程实现、使用用户线程实现和使用用户线程加轻量级进程混合实现。内核线程是直接由操作系统内核支持的线程,通过内核完成线程切换...
摘要:本文是从视角理解系统结构连载文章在高性能编程时经常接触到多线程起初我们的理解是多个线程并行地执行总比单个线程要快就像多个人一起干活总比一个人干要快然而实际情况是多线程之间需要竞争设备或者竞争锁资源,导致往往执行速度还不如单个线程在这里有一个 本文是从Java视角理解系统结构连载文章 在高性能编程时,经常接触到多线程. 起初我们的理解是, 多个线程并行地执行总比单个线程要快, 就像多个...
摘要:如问到是否使用某框架,实际是是问该框架的使用场景,有什么特点,和同类可框架对比一系列的问题。这两个方向的区分点在于工作方向的侧重点不同。 [TOC] 这是一份来自哔哩哔哩的Java面试Java面试 32个核心必考点完全解析(完) 课程预习 1.1 课程内容分为三个模块 基础模块: 技术岗位与面试 计算机基础 JVM原理 多线程 设计模式 数据结构与算法 应用模块: 常用工具集 ...
阅读 3454·2021-11-22 14:44
阅读 2629·2019-08-30 14:10
阅读 2744·2019-08-30 13:12
阅读 1296·2019-08-29 18:36
阅读 1426·2019-08-29 16:16
阅读 3411·2019-08-26 10:33
阅读 1893·2019-08-23 18:16
阅读 451·2019-08-23 18:12