当前位置:网站首页>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 .
边栏推荐
- How do I open an account on my mobile phone? Is it safe to open an account online now?
- 静态代码块永远先执行? 格局小了!!!
- How to implement two factor authentication MFA based on RADIUS protocol?
- 自动转换之-面试题
- Do static code blocks always execute first? The pattern is smaller!!!
- Stutter participle_ Principle of word breaker
- Stock suspension
- Wechat applet development log
- Boundary value analysis method for learning basic content of software testing (2)
- How to solve the problem of port number occupation
猜你喜欢

Prototype chain JS

The digital human industry is about to break out. What is the market pattern?
![1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation](/img/1a/162b060a6498e58278b6ca50e4953c.png)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation

Interpretation of new products: realm launched GT neo2 Dragon Ball customized version

小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App

Import and export of a single collection in postman

買賣股票費用計算

1181:整数奇偶排序

买卖股票费用计算

详解final、finally和finalize
随机推荐
Campus honey decoration of APP course design (e-commerce platform)
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
Automatic conversion - interview questions
Multithreading concurrent parallel threaded process
PMP考试重点总结九——收尾
Regular verification of mobile phone number and email [easy to understand]
Screen settings in the source code of OBS Live Room
Implementation of single sign on
1182: effets de la photo de groupe
The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution
4 methods for exception handling
Is it safe for Huatai Securities to open an account online? What is the handling process
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠
2020-10-27
怎样在手机上开户?现在网上开户安全么?
全链路业务追踪落地实践方案
Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
图解MySQL的binlog、redo log和undo log
买卖股票费用计算
自定义异常类及练习