当前位置:网站首页>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 .
边栏推荐
- 股票 停牌
- Check whether the table contains rows SQL Server 2005 - check whether a table contains rows or not SQL Server 2005
- Postman interface test
- Full link service tracking implementation scheme
- Implementation of single sign on
- DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation
- PMP考试重点总结九——收尾
- From knowledge to wisdom: how far will the knowledge map go?
- Import and export of a single collection in postman
- JVM系列(2)——垃圾回收
猜你喜欢

買賣股票費用計算
![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

函数的分文件编写

Rman Backup Report Ora - 19809 Ora - 19804

1182:合影效果
![[ybtoj advanced training guidance] class roll call [string hash]](/img/5b/bbf8fa51d180b50fbbee4bcc278c70.jpg)
[ybtoj advanced training guidance] class roll call [string hash]

Prototype chain JS

Apache Doris 成为 Apache 顶级项目

HDI的盲孔设计,你注意到这个细节了吗?

Use of Jasper soft studio report tool and solution of thorny problems
随机推荐
Data modeling based on wide table
01 distributed system overview
The concept of "tree structure" perfectly interprets the primary and secondary of things
Common test method used by testers --- orthogonal method
JDBC connection database (MySQL) steps
Two interview demo
How to solve the problem of port number occupation
redis5.0的槽点迁移,随意玩(单机迁移集群)
Which occupational groups are suitable for the examination
SQL 優化經曆:從 30248秒到 0.001秒的經曆
买卖股票费用计算
Summary of PMP learning experience
什么是在线开户?现在网上开户安全么?
A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
Is it safe to open an account for mobile phone stock speculation?
Importerror: no module named image [duplicate] - importerror: no module named image [duplicate]
From knowledge to wisdom: how far will the knowledge map go?
1182: effets de la photo de groupe
Methods for creating multithreads ---1 creating subclasses of thread class and multithreading principle
RESTful风格