当前位置:网站首页>Thread lifecycle
Thread lifecycle
2022-06-28 09:28:00 【The green flowers of Wang Li's family】
One 、 summary
The life cycle of a thread includes 5 Stages , Include : newly build 、 be ready 、 function 、 Blocking 、 Death .
When the thread enters the running state , The general operating system uses preemptive mode to let the thread get CPU. therefore CPU Need to switch between multiple threads , So the thread state will run many times 、 Blocking 、 Switch between ready .
Two 、 newly build
newly build : Use new Method ,new Come out of the thread , Only by JAVA The virtual machine allocates memory for it , And initialize the value of the member variable . This is just an object .
3、 ... and 、 be ready
be ready : It's the calling thread start() After the method , The thread is waiting CPU Resource allocation stage , Who robbed first CPU resources , Who is going to start ;
The thread is in ready state ,JAVA The virtual machine creates method call stacks and program counters for it . Thread execution is controlled by the underlying platform , It has a certain randomness .
Four 、 function
function : When the ready thread is scheduled and gets CPU Resource time , It goes into operation ,run Method defines the operation and function of the thread ;( When the thread in the ready state gets CPU, It will execute run() Method )
For a single core cpu( Or a kernel ) Come on , Only one instruction can be executed at the same time , and JVM By quickly switching threads to execute instructions to achieve multi-threaded , A real processor can process an instruction at the same time , But this switching speed is very fast , We don't even feel . In order to recover to the correct execution position after thread switching , Each thread has a separate program counter , Counters between threads do not affect each other , Independent storage .
When a thread starts running , It cannot always hold CPU( Unless the thread execution is very short , The execution is over in an instant ). therefore , The thread needs to be interrupted during execution , The purpose is to let other threads get execution CPU The opportunity of . The details of thread scheduling depend on the strategy adopted by the underlying platform .
5、 ... and 、 Blocking
Blocking : In the running state , For some reason, the running thread may become blocked . Here's why :
1. wait for I/O The input and output of the stream
2. Waiting for network resources , Network speed
3. call sleep() Method , Need to wait sleep Time is over
4. call wait() Method , Need to call notify() Wake up the thread
5. Other threads execute join() Method , The current thread will block , You need to wait for other threads to finish executing .
The state switching diagram is as follows :
6、 ... and 、 Death
1、run()/call() Method execution complete , Thread ends normally ;
2、 Thread throws an uncaught Exception or Error;
3、 Directly calling the thread stop() Method ends the thread —— This method is easy to cause deadlock , It is generally not recommended to use .
边栏推荐
- Decision table method for basic content learning of software testing (2)
- 手机炒股开户安不安全?
- P2394 yyy loves Chemistry I
- How to solve the problem of port number occupation
- 异常处理4种方法
- Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
- 基于宽表的数据建模
- The concept of "tree structure" perfectly interprets the primary and secondary of things
- Which securities company is better and safer to choose when opening an account for the inter-bank certificate of deposit fund with mobile phone
- Postman interface test
猜你喜欢

How to reduce the risk of project communication?

Test cases for learning the basic content of software testing (II)

全链路业务追踪落地实践方案

Illustration of MySQL binlog, redo log and undo log

SQL 优化经历:从 30248秒到 0.001秒的经历

This article explains in detail the difficult problems and solutions faced by 3D cameras

1. Kimball dimension modeling of data warehouse: what is a fact table?

RMAN backup message ora-19809 ora-19804

Deployment of MySQL database in Linux Environment

Machine virtuelle 14 installer win7 (tutoriel)
随机推荐
Test cases for learning the basic content of software testing (II)
1182:合影效果
数据挖掘建模实战
redis5.0的槽点迁移,随意玩(单机迁移集群)
虛擬機14安裝win7(圖教程)
1182:合影效果
2020-10-27
优秀笔记软件盘点:好看且强大的可视化笔记软件、知识图谱工具Heptabase、氢图、Walling、Reflect、InfraNodus、TiddlyWiki
104. maximum depth of binary tree
自动转换之-面试题
rman备份报ORA-19809 ORA-19804
RESTful风格
PMP needs to master its own learning methods
手机炒股开户安不安全?
PMP考试重点总结六——图表整理
Wechat applet development log
How to solve the problem of port number occupation
Apache Doris becomes the top project of Apache
DEJA_ Vu3d - 052 of cesium feature set - Simulation of satellite orbit (high altitude) effect
线程和进程