当前位置:网站首页>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 .
边栏推荐
- 静态代码块永远先执行? 格局小了!!!
- 自定义异常类及练习
- PMP考试重点总结九——收尾
- 函数的分文件编写
- The constructor is never executed immediately after new()!!!!!
- Differences between task parameter types inout and ref
- DEJA_ Vu3d - 052 of cesium feature set - Simulation of satellite orbit (high altitude) effect
- 1181: integer parity sort
- Campus honey decoration of APP course design (e-commerce platform)
- Android studio interview preparation
猜你喜欢
The concept of "tree structure" perfectly interprets the primary and secondary of things
1180:分数线划定/P1068 [NOIP2009 普及组] 分数线划定
Full link service tracking implementation scheme
Static page of pinyougou mall
How to reduce the risk of project communication?
PMP考试重点总结八——监控过程组(2)
Postman interface test
1182: group photo effect
From knowledge to wisdom: how far will the knowledge map go?
1181: integer parity sort
随机推荐
Data mining modeling practice
The private attribute of this class can be used directly? New() in use!!!
Resource scheduling and task scheduling of spark
Dbeaver连接人大金仓KingbaseES V8(超详细图文教程)
理解IO模型
PMP考试重点总结五——执行过程组
Divide and rule classic Hanoi
图解MySQL的binlog、redo log和undo log
自动转换之-面试题
Static page of pinyougou mall
静态代码块永远先执行? 格局小了!!!
小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
Machine virtuelle 14 installer win7 (tutoriel)
线程的生命周期
==和eqauls()的区别
PMP考试重点总结八——监控过程组(2)
The constructor is never executed immediately after new()!!!!!
Learn how Alibaba manages the data indicator system
2020-10-27
Full link service tracking implementation scheme