当前位置:网站首页>线程五种状态(线程生命周期)
线程五种状态(线程生命周期)
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 的一个普通方法调用,还是在主线程里执行。
边栏推荐
- C# 在PDF文档中应用多种不同字体
- WP super cache static cache plug-in concise tutorial
- Dominating set, independent set, covering set
- typescript 编译选项和配置文件
- Typescript compilation options and configuration files
- C # set or verify the format of text field in PDF
- [fragmentary thoughts] thoughts on wavelength, wave velocity and period
- web开发选型,web开发毕业谁
- 微信web开发者,如何学习web开发
- Learn 02 - slice, morphological change and dimension exchange of numpy multidimensional array
猜你喜欢

中文输入法输入事件composition的使用

软件项目管理 7.1.项目进度基本概念

让你理解选择排序(C语言)

C# 将OFD转为PDF

iframe 传值

JEST 单元测试说明 config.json

Node connects to MySQL database and writes fuzzy query interface

解决swagger文档接口404的问题

ELK - X-Pack设置用户密码

Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr
随机推荐
Weekly Postgres world news 2022w08
AGCO AI frontier promotion (6.11)
Count the top k strings with the most occurrences
发布WordPress数据库缓存插件:DB Cache Reloaded 3.1
Qt中radioButton使用
Etcd介绍
js合并两个对象(面试题)
C# 读取txt文件生成Word文档
[Chapter II Relationship between genes and chromosomes] summary of biological knowledge - Biology in grade one of senior high school
WP super cache static cache plug-in concise tutorial
JS addition and multiplication error resolution number precision
【Go】Gin源码解读
Jest unit test description config json
CVPR 2022 | 文本引导的实体级别图像操作ManiTrans
浙大联合微软亚研院发布视频识别新方法,可对视频逐帧识别且无需,数据标记,或可用于手语翻译等
[fragmentary thoughts] thoughts on wavelength, wave velocity and period
推荐几款Gravatar头像缓存插件
Hamiltonian graph
01_ Description object_ Class diagram
log4j-slf4j-impl cannot be present with log4j-to-slf4j