当前位置:网站首页>认识线程
认识线程
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状态。
边栏推荐
- 91. (cesium chapter) cesium rocket launch simulation
- The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
- 软考信息系统项目管理师_整理的十大管理过程的简写帮助记忆背诵---软考高级之信息系统项目管理师054
- 用JpaTransactionManager操作数据库事务
- Turn -- bring it and use it: share a gadget for checking memory leaks
- 转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
- [MySQL] index creation, viewing and deletion
- Detailed explanation of common configurations in redis configuration file [easy to understand]
- Cisco test -- the concept and configuration test of routing
- [机缘参悟-35]:鬼谷子-飞箝篇-远程连接、远程控制与远程测试之术
猜你喜欢
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
[image segmentation] 2021 segformer neurips
SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
Hide the creation and use of users
Introduction and use of plantuml
Summary of "performance testing" of software testing, novice will know the knowledge points on the road
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
随机推荐
Little red book scheme jumps to the specified page
Congratulations on the release of friends' new book (send welfare)
plain framework的实际应用和扩展
Armbain系统根分区空间不足处理
Share some feelings of a programmer who has experienced layoffs twice a year
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
[image segmentation] 2021 segformer neurips
Force buckle 710 Random numbers in the blacklist
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
众昂矿业:发展以氟化工为主的特色化工产业具有先天优势
Digital currency: far-reaching innovation
Use and function of spark analyze command map join broadcast join
Cisco test -- the concept and configuration test of routing
AAAI22 | 结构标记和交互建模:用于图分类的“SLIM”网络
[MySQL] index creation, viewing and deletion
Today's sleep quality record 71 points
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Jielizhi Bluetooth headset quality control and production skills [chapter]
软考信息系统项目管理师_整理的十大管理过程的简写帮助记忆背诵---软考高级之信息系统项目管理师054
Detailed explanation of common configurations in redis configuration file [easy to understand]