当前位置:网站首页>Yield method of tread
Yield method of tread
2022-07-06 07:06:00 【chengqiuming】
One The finishing touch
yiled Method is a heuristic method , It will remind the scheduler that I am willing to give up the current CPU free , If CPU Our resources are not tight , Will ignore this reminder .
call yield Method causes the current thread to change from RUNNING The state switches to RUNNABLE state , Generally, this method is not commonly used .
Two actual combat
1 Code
package concurrent;
import java.util.stream.IntStream;
public class ThreadYield {
public static void main(String[] args) {
IntStream.range(0, 2).mapToObj(ThreadYield::create).forEach(Thread::start);
}
private static Thread create(int index) {
return new Thread(() -> {
// if (index == 0) {
// Thread.yield();
// }
System.out.println(index);
});
}
}
2 test
The above program runs many times , You will find that the output results are inconsistent , Sometimes it's printing 0, Sometimes it's printing 1.
Open the comment and then run , You will find that the output results are still inconsistent , Sometimes it's printing 0, Sometimes it's printing 1.
3 explain
In general , Because if the first thread obtains CPU resources , It will be modest , Initiative to tell CPU The scheduler releases its own resources , however yield Just a hint ( hint ),CPU The scheduler does not guarantee that it will meet every time yield Tips .
yield The procedure is not controllable , So this method is not commonly used .
3、 ... and yield and sleep Comparison of
- sleep Causes the current thread to pause for a specified time , No, CPU Time slice consumption .
- yield Just for CPU A hint from the scheduler , If CPU The scheduler did not ignore this prompt , It will lead to the switching of thread context .
- sleep Makes the thread short block, Will be released within the specified time CPU resources .
- yield Can make RUNNING State of Thread Get into RUNNABLE state ( If CPU If the scheduler does not ignore this prompt )
- sleep Almost 100% complete the sleep for a given time , and yield Your prompt does not necessarily guarantee .
- One thread sleep Another thread called interrupt An interrupt signal will be captured , and yield Will not be .
边栏推荐
- Leetcode 78: subset
- Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
- Latex文字加颜色的三种办法
- 漏了监控:Zabbix对Eureka instance状态监控
- 从autojs到冰狐智能辅助的心里历程
- [server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
- 软件测试外包到底要不要去?三年真实外包感受告诉你
- The difference between get and post request types
- Applied stochastic process 01: basic concepts of stochastic process
- GET 和 POST 请求类型的区别
猜你喜欢
Fedora/rehl installation semanage
Thought map of data warehouse construction
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
C语言_双创建、前插,尾插,遍历,删除
开源的网易云音乐API项目都是怎么实现的?
1189. Maximum number of "balloons"
Fast target recognition based on pytorch and fast RCNN
漏了监控:Zabbix对Eureka instance状态监控
微信公众号无限回调授权系统源码 全网首发
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
随机推荐
C language_ Double create, pre insert, post insert, traverse, delete
[some special grammars about C]
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
librosa音频处理教程
Proteus -- Serial Communication parity flag mode
AI on the cloud makes earth science research easier
漏了监控:Zabbix对Eureka instance状态监控
What is the biggest problem that fresh e-commerce is difficult to do now
Applied stochastic process 01: basic concepts of stochastic process
配置树莓派接入网络
[advanced software testing step 1] basic knowledge of automated testing
How to reconstruct the class explosion caused by m*n strategies?
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
Leetcode59. spiral matrix II (medium)
kubernetes集群搭建Zabbix监控平台
LeetCode 78:子集
编译,连接 -- 笔记 -2
【Hot100】739. Daily temperature