当前位置:网站首页>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 .
边栏推荐
- Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
- 前缀和数组系列
- 开源的网易云音乐API项目都是怎么实现的?
- “无聊猿” BAYC 的内忧与外患
- Pymongo gets a list of data
- WPF之MVVM
- win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
- Database basics exercise part 2
- 医疗软件检测机构怎么找,一航软件测评是专家
- leetcode59. 螺旋矩阵 II(中等)
猜你喜欢
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
18.多级页表与快表
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Simple use of MySQL database: add, delete, modify and query
What is the biggest problem that fresh e-commerce is difficult to do now
LeetCode 78:子集
SEO学习的最好方式:搜索引擎
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
idea控制台彩色日志
随机推荐
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
作者已死?AI正用藝術征服人類
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
leetcode6109. 知道秘密的人数(中等,周赛)
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
[hot100] 739. Température quotidienne
Development of entity developer database application
Arduino tutorial - Simon games
UWA pipeline version 2.2.1 update instructions
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
Top test sharing: if you want to change careers, you must consider these issues clearly!
Zhongqing reading news
【Hot100】739. 每日溫度
微信脑力比拼答题小程序_支持流量主带最新题库文件
攻防世界 MISC中reverseMe简述
Three methods of adding color to latex text
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
同事上了个厕所,我帮产品妹子轻松完成BI数据产品顺便得到奶茶奖励