当前位置:网站首页>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;
}
}
}
}
边栏推荐
- Pandora IOT development board learning (HAL Library) - Experiment 9 PWM output experiment (learning notes)
- Ks003 mall system based on JSP and Servlet
- SAP ALV cell level set color
- [prediction model] difference method model
- ESBuild & SWC浅谈: 新一代构建工具
- 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
- Pytoch foundation - (2) mathematical operation of tensor
- 阿里测试师用UI自动化测试实现元素定位
- MySQL reads missing data from a table in a continuous period of time
- How to modify field constraints (type, default, null, etc.) in a table
猜你喜欢

1、工程新建

Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation

Blue style mall website footer code

ESBuild & SWC浅谈: 新一代构建工具

MySQL reads missing data from a table in a continuous period of time

C (thirty) C combobox listview TreeView

No qualifying bean of type ‘......‘ available

Python implementation of maddpg - (1) openai maddpg environment configuration

Canvas cut blocks game code

SAP ALV cell level set color
随机推荐
C mouse event and keyboard event of C (XXVIII)
施努卡:3d视觉检测应用行业 机器视觉3d检测
[prediction model] difference method model
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
Introduction to DeNO
Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
BUAA calculator (expression calculation - expression tree implementation)
C language -- structs, unions, enumerations, and custom types
Shell pass parameters
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
2.2 STM32 GPIO operation
【可调延时网络】基于FPGA的可调延时网络系统verilog开发
C#(三十一)之自定义事件
Cf464e the classic problem [shortest path, chairman tree]
Mathematical modeling regression analysis relationship between variables
How to standardize the deployment of automated testing?
mysql从一个连续时间段的表中读取缺少数据
2.13 weekly report
Introduction to data types in MySQL
C#(三十)之C#comboBox ListView treeView