当前位置:网站首页>线程五种状态(线程生命周期)
线程五种状态(线程生命周期)
2022-06-11 11:43:00 【java-zh】
创建线程的四种方式代码:创建线程的四种方式_java-zh的博客-CSDN博客
1、新建状态
当创建好当前对象之后,没有启动之前(没有调用start方法),比如Thread t = new Thread();
2、就绪状态
准备开始执行,并没有执行,表示调用start方法之后,未获取到cpu资源
3、运行状态
当前进行获取到cpu资源之后
4、死亡状态:
当运行中的线程正常执行完所有的代码逻辑或因为异常情况导致程序结束叫死亡状态
5、阻塞状态
在程序运行过程中,发生某些情况,导致当前线程无法顺利执行,此时会进入阻塞状态,进入阻塞状态的
原因消除以后,所有的阻塞队列会再次进入到就绪状态,随机抢占cpu资源,等待执行
注意点
为什么我们调用 start() 方法时会执行 run() 方法,为什么我们不能直接调用 run() 方法?
new 一个 Thread,线程进入了新建状态。调用 start() 方法,会启动一个线程并使线程进入了就绪状态,当分配到时间片后就可以开始运行了。 start() 会执行线程的相应准备工作,然后自动执行 run() 方法的内容,这是真正的多线程工作。
而直接执行 run() 方法,会把 run 方法当成一个 main 线程下的普通方法去执行,并不会在某个线程中执行它,所以这并不是多线程工作。
总结: 调用 start 方法方可启动线程并使线程进入就绪状态,而 run 方法只是 thread 的一个普通方法调用,还是在主线程里执行。
边栏推荐
- WordPress database cache plug-in: DB cache Reloaded
- my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF
- 2020-07 学习笔记整理
- 中级web开发工程师,面试题+笔记+项目实战
- Runtime reconfiguration of etcd
- 采用快慢指针法来解决有关数组的问题(C语言)
- WordPress数据库缓存插件:DB Cache Reloaded
- Intl.NumberFormat 设置数字格式
- Iframe value transfer
- 【LeetCode】494. Objective and (2 wrong questions)
猜你喜欢

Read geo expression matrix

JS 加法乘法错误解决 number-precision

Notes on topic brushing (XIV) -- binary tree: sequence traversal and DFS, BFS

Use compiler option ‘--downlevelIteration‘ to allow iterating of iterators 报错解决

广东市政安全施工资料管理软件2022新表格来啦

Template engine - thymeleaf
![[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)](/img/30/79516390c2b2b50a224eaa84a0c1c9.jpg)
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)

浙大联合微软亚研院发布视频识别新方法,可对视频逐帧识别且无需,数据标记,或可用于手语翻译等

01_ Description object_ Class diagram
![[JUC supplementary] immutable object, shared meta mode, final principle](/img/c1/c29229108a3f66b83d13b4d90d49f7.jpg)
[JUC supplementary] immutable object, shared meta mode, final principle
随机推荐
Let WordPress support registered users to upload custom avatars
P2580 "so he started the wrong roll call"
Qt中radioButton使用
Is the SSL certificate reliable in ensuring the information security of the website?
AGCO AI frontier promotion (6.11)
C# 设置或验证 PDF中的文本域格式
Full Permutation (recursion, backtracking)
在畢設中學習03
【LeetCode】1049. Weight of the last stone II (wrong question 2)
MYCAT sub database and sub table
The role of Gerber file in PCB manufacturing
[fragmentary thoughts] thoughts on wavelength, wave velocity and period
ELK - ElastAlert最大的坑
Modify WordPress management account name plug-in: admin rename extended
推荐几款Gravatar头像缓存插件
Elk - hearthbeat implements service monitoring
arguments. Callee implement function recursive call
WordPress landing page beautification plug-in: recommended by login Designer
苹果MobileOne: 移动端仅需1ms的高性能骨干
JS merge two objects (interview questions)