当前位置:网站首页>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 .
边栏推荐
- Cookie技术&Session技术&ServletContext对象
- Misc of BUU (update from time to time)
- Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
- 19.段页结合的实际内存管理
- leetcode1020. Number of enclaves (medium)
- Latex文字加颜色的三种办法
- 巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
- win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
- 简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
- 树莓派3B更新vim
猜你喜欢
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
leetcode1020. 飞地的数量(中等)
Hydra common commands
数据仓库建设思维导图
Simple use of MySQL database: add, delete, modify and query
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
前缀和数组系列
leetcode841. 钥匙和房间(中等)
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
C language_ Double create, pre insert, post insert, traverse, delete
随机推荐
leetcode704. Binary search (find an element, simple, different writing)
Fast target recognition based on pytorch and fast RCNN
BIO模型实现多人聊天
Configure raspberry pie access network
Do you really know the use of idea?
Top test sharing: if you want to change careers, you must consider these issues clearly!
The author is dead? AI is conquering mankind with art
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
[some special grammars about C]
RichView TRVStyle 模板样式的设置与使用
The difference between get and post request types
从autojs到冰狐智能辅助的心里历程
19. Actual memory management of segment page combination
Idea console color log
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
[daily question] 729 My schedule I
软件测试外包到底要不要去?三年真实外包感受告诉你
GET 和 POST 请求类型的区别
攻防世界 MISC中reverseMe简述
TS Basics