当前位置:网站首页>Thread sleep, thread sleep application scenarios
Thread sleep, thread sleep application scenarios
2022-07-06 03:48:00 【Hao Kai】
Thread to sleep 、 Thread hibernation application scenario
Thread to sleep sleep
- sleep Specifies the number of milliseconds the current thread is blocking ,1000ms=1s
- sleep There is abnormal InterruptedException
- sleep When the time is up, the thread is ready
- sleep It can simulate network delay 、 Countdown, etc
- Every object has a lock ,sleep It won't release the lock
Role of thread hibernation
Analog network delay : Magnify the occurrence of the problem
package testthread;
//sleep Analog network delay : Magnify the occurrence of the problem , Simulate the concurrency of ticket grabbing
public class TestSleep implements Runnable {
// Number of votes
private Integer ticketNums = 10;
@Override
public void run() {
while (true) {
if (ticketNums <= 0) {
break;
}
// Analog delay
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " Got the first " + ticketNums-- + " Tickets ");
}
}
public static void main(String[] args) {
TestSleep testSleep = new TestSleep();
new Thread(testSleep, " Xiaohong ").start();
new Thread(testSleep, " Xiao Ming ").start();
new Thread(testSleep, " Cattle ").start();
/* Output : Xiao Ming grabbed the first 10 Tickets Scalper grabbed the first place 9 Tickets Xiao Hong grabbed the first 10 Tickets Scalper grabbed the first place 8 Tickets Xiao Hong grabbed the first 8 Tickets Xiao Ming grabbed the first 7 Tickets Scalper grabbed the first place 6 Tickets Xiao Ming grabbed the first 4 Tickets Xiao Hong grabbed the first 5 Tickets Xiao Hong grabbed the first 3 Tickets Scalper grabbed the first place 3 Tickets Xiao Ming grabbed the first 3 Tickets Scalper grabbed the first place 2 Tickets Xiao Hong grabbed the first 2 Tickets Xiao Ming grabbed the first 2 Tickets Xiao Ming grabbed the first 1 Tickets Scalper grabbed the first place 1 Tickets Xiao Hong grabbed the first 1 Tickets */
}
}
Analog countdown
There is a super classic passage , That is to say, the boss will output the current time of one day , Then use in the code sleep Method thread has been dormant for a whole 24 Hours and then output .
package testthread;
//sleep Analog countdown :
public class TestSleep2 {
public static void main(String[] args) throws InterruptedException {
int countDown = 10;
while (true) {
Thread.sleep(1000);
System.out.println(countDown--);
if (countDown <= 0) {
break;
}
}
}
}
边栏推荐
- Data analysis Seaborn visualization (for personal use)
- 【Rust 笔记】18-宏
- Ks003 mall system based on JSP and Servlet
- C#(二十九)之C#listBox checkedlistbox imagelist
- On Data Mining
- Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
- Recommended papers on remote sensing image super-resolution
- Simple blog system
- How to modify field constraints (type, default, null, etc.) in a table
- 2.13 weekly report
猜你喜欢
随机推荐
BUAA计算器(表达式计算-表达式树实现)
MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
BUAA magpie nesting
Teach you to build your own simple BP neural network with pytoch (take iris data set as an example)
UDP reliable transport protocol (quic)
C#(三十)之C#comboBox ListView treeView
C (thirty) C combobox listview TreeView
潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
C#(二十七)之C#窗体应用
Python implementation of maddpg - (1) openai maddpg environment configuration
Conditionally [jsonignore]
Ybtoj coloring plan [tree chain dissection, segment tree, tarjan]
Introduction to DeNO
[Qt5] QT QWidget immediately appears and disappears
【按键消抖】基于FPGA的按键消抖模块开发
Mapping between QoE and KQI
[slam] orb-slam3 parsing - track () (3)
Blue style mall website footer code
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did