当前位置:网站首页>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;
}
}
}
}
边栏推荐
- 3分钟带你了解微信小程序开发
- C mouse event and keyboard event of C (XXVIII)
- 遥感图像超分辨重建综述
- LTE CSFB test analysis
- Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
- Differential GPS RTK thousand search
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Deno介绍
- Suggestions for new engineer team members
- Shell pass parameters
猜你喜欢
C (thirty) C combobox listview TreeView
Simple blog system
No qualifying bean of type ‘......‘ available
RT thread -- FTP of LwIP (2)
MySQL about self growth
[practice] mathematics in lottery
Teach you to build your own simple BP neural network with pytoch (take iris data set as an example)
SWC introduction
C language -- structs, unions, enumerations, and custom types
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
随机推荐
User experience index system
2.2 fonctionnement stm32 GPIO
Record the process of reverse task manager
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
BUAA calculator (expression calculation - expression tree implementation)
3.2 detailed explanation of rtthread serial port device (V2)
Microkernel structure understanding
WPF效果第一百九十一篇之框选ListBox
MySQL reads missing data from a table in a continuous period of time
Suggestions for new engineer team members
Flask learning and project practice 8: introduction and use of cookies and sessions
Basic concepts of LTE user experience
1、工程新建
Factors affecting user perception
Overview of super-resolution reconstruction of remote sensing images
MySQL about self growth
1. New project
Pytorch load data
多项目编程极简用例
In Net 6 CS more concise method