当前位置:网站首页>121. thread scheduling: join() method and yield() method
121. thread scheduling: join() method and yield() method
2022-07-01 09:28:00 【Hip hop geek】
Thread scheduling
【 Introduce 】
Thread scheduling refers to 【 Anything that can affect threads 】 Of 【 state 】 Methods , It becomes the scheduling method ;
【 Method 】
<1>start newly build --> be ready
<2>sleep function --> Blocking Will throw compilation exceptions , Need to capture ;
<3>join Join in 、 Merge function --> Blocking
<4>yield Comity function --> be ready
<5>wait wait for function --> Blocking
<6>notify/nofifyall Wake up the Blocking --> be ready
The five states of a thread : Namely be ready --> function --> Blocking
[1]join()
Method Example method , Join in 、 Merge 、 Jump the queue
In the current thread , Execute another thread's join Method , Then the current thread will block , Wait for the inserted thread to finish executing , Will enter the ready state from the blocked state , Rejoin CPU To rob ; Similar to the scene of queue jumping in real life ;
Preemption of ready threads occurs in Any period ;
class Demo{
public static void main(String[] args) throws InterruptedException {
//sleep() Static methods
// Thread pause 1 second 1000 millisecond
Thread.sleep(1000);
//join() Method
// Execute another thread in the current thread join Method , Then the current thread will block
// Wait for the inserted thread to finish executing , Will enter the ready state from the blocked state , Rejoin CPU To rob
// Similar to the scene of queue jumping in real life
// Preemption of threads in ready state occurs at any time ;
Thread thread = new Thread(()->{
for(char c = 'a';c <= 'z';c++){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" Insert thread :"+c);
}
});
// Let another thread , Join in
// Set the priority of the thread
thread.setPriority(3);
thread.start();
// First thread
Thread thread1 = new Thread(()->{
for(int i=1;i<100;i++){
if(i==3){
try {
// take thread Threads merge into thread1 In the thread , Will thread Blocking
// That is, when the current thread is executing , The current thread is blocked , Then another thread executes ;
// When the inserted thread finishes executing, it will notify the thread , From blocked to ready
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(" First thread :"+i);
}
});
// Start thread
thread1.start();
}
}
[2]yield()
Example method , Comity Change the state of the thread from running state to ready state ;
It can make The currently running thread is paused , however Will not let the current thread block , And let the current thread enter Ready state ,
actually : Threads execute yield after , Only Higher or the same priority as the current thread Only then has the opportunity to participate in the snatch CPU, And the current thread will also participate in the snatch ;
give an example :QQ Red envelopes are issued in groups , The person who sends the red packet is the current running thread , Whether the person who sends the red envelope can participate in the grab of the red envelope , call yield Method , In fact, the person who sent the red envelope also participated in the looting ;
Give Way CPU A little free , Give the chance to other threads ;
class Demo2{
public static void main(String[] args) {
// demonstration yield Comity
//1. First thread
Thread thread = new Thread(()->{
for(int i=1;i<20;i++){
if(i%3==0){
// By 3 to be divisible by , Just give in once
Thread.yield();
// This thread will be suspended at the time of comity , From running state to ready state ;
// But it does not affect the next time the thread continues to rob CPU;
// That is to say, although I have been courteous , But next time, you may get it yourself CPU Of
}
System.out.println(" First thread :"+i);
}
});
// Start thread
thread.start();
//2. The second thread
Thread thread1 = new Thread(()->{
for(int i=100;i<120;i++){
System.out.println(" The second thread :"+i);
}
});
thread1.start();
}
}
【sleep() And join() And yield() The difference between 】
Reference article :https://blog.csdn.net/xiangwanpeng/article/details/54972952
yield() Methods and sleep() The method is similar to , Will not release the lock mark , The difference lies in yield() Method enables the thread to return to Executable state , So execute yield() It is possible for a thread of to enter the executable state Right away Be performed , in addition yield() Method can only make threads with the same priority or high priority get execution opportunities ; and sleep() Method Will make all priorities available ; and join() The method is Get in the jam , Wait until the inserted thread finishes executing , The suspended thread changes from the blocking state to the ready state , Participating in thread snatching ;
边栏推荐
- In the middle of the year, where should fixed asset management go?
- Is it safe to dig up money and make new shares
- 【leetcode】287. Find duplicates
- js原型继承仅可继承实例而非构造器
- Rich text interpolation
- ES6 const essence and completely immutable implementation (object.free)
- 【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
- 【pytorch】2.4 卷积函数 nn.conv2d
- es6-顶层对象与window的脱钩
- SQL学习笔记(03)——数据约束关系
猜你喜欢
[interview brush 101] linked list
Bird recognition app
[pytorch] 2.4 convolution function nn conv2d
ESP8266 FreeRTOS开发环境搭建
Preparing for the Blue Bridge Cup -- bit operation
OSPF - virtual link details (including configuration commands)
2022.02.15_ Daily question leetcode six hundred and ninety
delete和delete[]引发的问题
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
Nacos service configuration and persistence configuration
随机推荐
Why is the Ltd independent station a Web3.0 website!
Mysql8.0 learning record 17 -create table
js原型继承仅可继承实例而非构造器
Principle and application of single chip microcomputer timer, serial communication and interrupt system
Log4j log framework
微信小程序 webview 禁止页面滚动,同时又不影响业务内overflow的滚动的实现方式
Understanding and implementation of AVL tree
Set the type of the input tag to number, and remove the up and down arrows
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
JS variable lifting
Record a redis timeout
2022.02.15_ Daily question leetcode six hundred and ninety
js函数arguments对象
Get the list of a column in phpexcel get the letters of a column
LeetCode 344. Reverse string
js重写自己的函数
How to realize the usage of connecting multiple databases in idel
Understand shallow replication and deep replication through code examples
PHP merges multiple arrays. The same element takes the intersection of different elements to form an array
Exception handling of classes in C #