当前位置:网站首页>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 .
边栏推荐
- Simple use of JWT
- Interface automation test framework: pytest+allure+excel
- Idea console color log
- leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
- 顶测分享:想转行,这些问题一定要考虑清楚!
- 将ue4程序嵌入qt界面显示
- C language_ Double create, pre insert, post insert, traverse, delete
- “无聊猿” BAYC 的内忧与外患
- Apache dolphin scheduler source code analysis (super detailed)
- 漏了监控:Zabbix对Eureka instance状态监控
猜你喜欢
Pallet management in SAP SD delivery process
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
Leetcode 78: subset
顶测分享:想转行,这些问题一定要考虑清楚!
C language_ Double create, pre insert, post insert, traverse, delete
Development of entity developer database application
ROS学习_基础
Prefix and array series
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
RichView TRVStyle 模板样式的设置与使用
随机推荐
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
LeetCode 78:子集
OpenGL ES 学习初识(1)
软件测试外包到底要不要去?三年真实外包感受告诉你
前缀和数组系列
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
作者已死?AI正用艺术征服人类
C语言_双创建、前插,尾插,遍历,删除
攻防世界 MISC中reverseMe简述
首发织梦百度推送插件全自动收录优化seo收录模块
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
[hot100] 739. Température quotidienne
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
Establishment and operation of cloud platform open source project environment
[brush questions] how can we correctly meet the interview?
19. Actual memory management of segment page combination
Fast target recognition based on pytorch and fast RCNN
树莓派串口登录与SSH登录方法
Internal and external troubles of "boring ape" bayc