当前位置:网站首页>认识线程
认识线程
2022-07-01 22:26:00 【InfoQ】
认识线程
线程的状态
- NEW,新建状态:线程被创建出来,但尚未启动时的线程状态
- RUNNABLE,就绪状态:表示线程可以运行的状态,它可能正在运行,或者是在排队等待操作系统分配 CPU 资源;
- BLOCKED,阻塞等待锁的线程状态:表示处于阻塞状态的线程,正在等待锁释放,比如等待执行 synchronized 代码块或者使用 synchronized 标记的方法,但此时已经被其他线程执行,所以处于阻塞等待。
- WAITING,等待状态:一个线程正在等待另一个线程执行某个特定的动作,这个线程就处于等待状态。比如一个线程调用了 Object.wait() ,那它在等待另一个线程调用 Object.notify() 或 Object.notifyAll() 方法时所处的状态,称之为等待状态。
- TIMED_WAITING,超时等待状态:当进行等待状态时,会设置一个超时时间,当等待时长超过规定的时间后,线程所处的状态就称为超时等待状态。比如 Object.wait(long timeout) 和 Thread.join(long timeout) 等方法,就会让线程有可能进入此状态。
- TERMINATED,终止状态:表示线程已经执行完成。
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED
}
线程的工作模式
- 首先创建线程,此时线程处于NEW状态。
- 指定线程需要执行的业务方法,此时线程处于RUNNABLE状态
- 线程执行过程中,如果遇到有同步代码块,并且其他线程先一步执行,则当前线程则会进入BLOCKED状态。待其他线程执行完成同步代码块之后,当前线程会继续等待。
- 如果线程中代码有调用Object.wait()方法或者Thread.join()方法时,线程会进入WAITING状态,并且当有设置超时时间时,则线程会进入TIMED_WAITING等待状态。直到有其他线程执行了notify() 或 notifyAll() 方法,当前线程才会被唤醒,继续执行代码。
- 最后线程方法执行完成,线程也就变成了TERMINATED状态。
边栏推荐
- ECMAScript 2022 was officially released. Have you heard about it?
- 有些能力,是工作中学不来的,看看这篇超过90%同行
- Favorite transaction code management tool in SAP GUI
- Business visualization - make your flowchart'run'up
- 【Swoole系列1】在Swoole的世界中,你将学习到什么?
- 数字货币:影响深远的创新
- Force buckle 710 Random numbers in the blacklist
- Some abilities can't be learned from work. Look at this article, more than 90% of peers
- 14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
- 激发新动能 多地发力数字经济
猜你喜欢
Cisco test -- the concept and configuration test of routing
Congratulations on the release of friends' new book (send welfare)
【嵌入式系统课设】单个按键控制LED灯
Turn -- bring it and use it: share a gadget for checking memory leaks
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Cut noodles C language
internal field separator
正则系列之量词(Quantifiers)
Jielizhi Bluetooth headset quality control and production skills [chapter]
rviz打开后如何显示实时2D地图
随机推荐
Cisco test -- the concept and configuration test of routing
Introduction and use of plantuml
“信任机器”为发展赋能
Jerry's records are powered by Vbat with a power supply voltage of 4.2V [chapter]
Sogou wechat app reverse (II) so layer
Flynk SQL client uses comparison and is familiar with official documents
微服务服务稳定性治理
[daily training] 66 add one-tenth
Cisco exam -- redundant network
数字化转型道阻且长,如何迈好关键的第一步
思科--高可用和高可靠网络考试
el-input文本域字数限制,超过显示变红并禁止输入
Jielizhi, production line assembly link [chapter]
使用 EMQX Cloud 实现物联网设备一机一密验证
Stimulate new kinetic energy and promote digital economy in multiple places
力扣 710. 黑名单中的随机数
Fiori applications are shared through the enhancement of adaptation project
You probably haven't noticed the very important testing strategy in your work
map容器
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code