当前位置:网站首页>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 .
边栏推荐
- TS Basics
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- Arduino tutorial - Simon games
- Pallet management in SAP SD delivery process
- 基于PyTorch和Fast RCNN快速实现目标识别
- Every API has its foundation when a building rises from the ground
- LeetCode Algorithm 2181. 合并零之间的节点
- leetcode1020. Number of enclaves (medium)
- Database basics exercise part 2
- 19. Actual memory management of segment page combination
猜你喜欢

leetcode841. Keys and rooms (medium)

ROS learning_ Basics

Establishment and operation of cloud platform open source project environment

Reflex WMS medium level series 3: display shipped replaceable groups

How to find a medical software testing institution? First flight software evaluation is an expert

Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)

Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes

LeetCode 78:子集

Short video, more and more boring?

MVVM of WPF
随机推荐
【Hot100】739. 每日温度
Top test sharing: if you want to change careers, you must consider these issues clearly!
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
18.多级页表与快表
19. Actual memory management of segment page combination
作者已死?AI正用藝術征服人類
C language_ Double create, pre insert, post insert, traverse, delete
从autojs到冰狐智能辅助的心里历程
leetcode1020. Number of enclaves (medium)
Establishment and operation of cloud platform open source project environment
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
Misc of BUU (update from time to time)
MPLS experiment
首发织梦百度推送插件全自动收录优化seo收录模块
[brush questions] how can we correctly meet the interview?
微信公众号无限回调授权系统源码 全网首发
呆错图床系统源码图片CDN加速与破解防盗链功能
树莓派3B更新vim
[advanced software testing step 1] basic knowledge of automated testing
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm