当前位置:网站首页>Common thread scheduling methods
Common thread scheduling methods
2022-06-22 10:51:00 【It midsummer fruit】
Catalog
Four Request surrender CPU Executive power
5、 ... and Thread the interrupt
One Thread waiting
1 wait(): When a thread calls wait() After the method , This thread will be blocked and suspended , It will not return until the following conditions occur
1) The thread called notify() perhaps notifyAll() Wake up the way
2) Another thread called the thread's interrupt() Interrupt method , Thread throws InterruptedException Exception return .
2 wait(long timeout): This method is better than wait() Method has one more timeout parameter timeout, If the thread calls this method , Not in the specified timeout Wake up in time , Then this method will return because of timeout .
3 wait(long timeout,int nacos): and wait(long timeout) similar , It also has a timeout wait, It's also called internally wait(long timeout) Method
4 join(): Let the parent thread wait for the child thread to finish before continuing execution , That is, when we call a thread's join() When the method is used , This method suspends the calling thread , Until the called thread ends execution , The calling thread will continue to execute .
Two Thread notification
1 notify(): When a thread calls notify() After the method , Will wake up a call on the lock resource wait() Thread suspended after method . A lock resource may have multiple threads waiting , Which waiting thread to wake up is random .
2 notifyAll():notify() Is to wake up a waiting thread , and notify() Method will wake up all the resources on the lock due to the call wait() Method .
3、 ... and Thread to sleep
1 sleep(long millis):sleep The method is Thread Class static methods , When an executing thread calls sleep() After the method , This thread will temporarily give up the specified time CPU Executive power , But the monitor resources owned by the thread , For example, the lock is still held . When the designated sleep time is up sleep() Method will return normally , Then participate in the call CPU The scheduling , Get CPU Resource will continue to execute .
Four Request surrender CPU Executive power
1 yield(): yes Thread Static methods in a class , When a thread calls yield After the method , In fact, it tells the thread scheduler that the current thread requests to give up its own CPU, Let yourself or another thread run , But the thread scheduler can unconditionally ignore this hint .
Make current thread from execution state ( Running state ) Become executable ( Ready state ).CPU Will choose from a number of executable states . That is to say, calling yield() The thread of the method may be executed again , It does not mean that other threads will be executed and this thread will not be executed next time , Depends on the choice of the thread scheduler .
demo demonstration :
public class YieldDemo extends Thread{
public YieldDemo(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.getName()+"-----"+i);
if (i == 5 ) {
System.out.println(""+this.getName()+"-- Request the thread scheduler to give up CPU The execution right is given to other threads or executed by itself ");
Thread.yield();
}
}
}
public static void main(String[] args) {
new YieldDemo(" Zhang San ").start();
new YieldDemo(" Li Si ").start();
}
}Results, :

5、 ... and Thread the interrupt
1 void interrupt(): How to interrupt a thread , The interrupt flag of this thread will be set to true, The result of the interrupt thread is death , Or wait for a new task or continue to run the next step , It depends on the program itself . From time to time, the thread will detect that the interrupt flag bit is true, To determine whether the thread should be interrupted .
for example : When a thread A Runtime , Threads B You can call interrupt() Method to set the interrupt flag of the thread to true And return immediately . Setting a sign is just setting a sign , Threads A Actually not interrupted , Will continue down .
2 boolean isInterrupted(): Determine whether a thread has sent an interrupt request , That is, whether the current thread is interrupted ,isInterrupted() The interrupt flag bit will not be cleared .
3 boolean interrupted(): Detect whether the current thread is interrupted , And isInterrupted The difference is , If the method finds that the current thread is interrupted , The interrupt flag is cleared .isInterrupted() Will clear interrupt flag bit , Set it to false.
边栏推荐
- [backtrader source code analysis 51] simple interpretation of the source code of seven files in observers (boring, for reference only)
- 大学生期末考试SQL语句
- Thinkphp3.2.3 log inclusion analysis
- Heidisql inserts records. There are always errors. How do you change them?
- libevent的使用
- 无线802.1x认证简介及配置方法
- Career development planning
- On the routing tree of gin
- 【这款工具配合jmeter,会让你的工作效率至少提升80%,强烈推荐给大家】
- jg_使用easyexcel读取excel_20220619
猜你喜欢
![[deep learning] tensorflow, danger! Google itself is the one who abandoned it](/img/f2/c6a0de28df4b712f9baf89dabd072a.png)
[deep learning] tensorflow, danger! Google itself is the one who abandoned it

Gartner said: cloud database is developing strongly, but local database is still full of vitality

世界上第一个“半机械人”去世,改造自己只为“逆天改命”

Pytorch实现波阻抗反演
![[this tool, combined with JMeter, will increase your work efficiency by at least 80%, which is highly recommended]](/img/44/600d886a5dedd7a27ed923f6ffef53.png)
[this tool, combined with JMeter, will increase your work efficiency by at least 80%, which is highly recommended]
推荐一款M1芯片电脑快速搭建集群的虚拟机软件

WordPress like hooks and filters in laravel
![[graduation season · advanced technology Er] youth never ends](/img/de/b17460d1a702d56cf67d9df1f8f284.png)
[graduation season · advanced technology Er] youth never ends

喜讯丨敏捷科技入选《2022年中国数字安全百强报告》
![[LineCTF2022]BB](/img/9d/c5b0ce1e603d40efc5245ba074aa77.png)
[LineCTF2022]BB
随机推荐
等重构完这系统,我就提离职!
[deep learning] great! The new attention makes the model 2-4 times faster!
libevent的使用
TCP异常连接
AQS的初步了解
2022年盲盒软件开发商业模式深度解析
[this tool, combined with JMeter, will increase your work efficiency by at least 80%, which is highly recommended]
2022陕西省安全员C证考试题库模拟考试平台操作
Force buckle 1108 IP address invalidation
nvm use之后重新打开终端还是显示修改之前的版本
HMS Core新闻行业解决方案:让技术加上人文的温度
Learn to view object models with VisualStudio developer tools
The future of Dao: an organization primitive for building Web3
Encryption market plummeted, Seth triggered a new round of concern
Byte 2: why is the key of the redis master node expired, but the expired data is still read from the secondary node? How to solve it?
[机缘参悟-28]:鬼谷子-内揵篇-保全自己,说服上司
论文精读:Generative Adversarial Imitation Learning(生成对抗模仿学习)
字节三面:到底知不知道什么是Eureka?
外贸专题:外贸邮件营销模板
解决win7任务栏谷歌浏览器chrome图标丢失、异常空白的问题