当前位置:网站首页>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;
}
}
}
}
边栏推荐
- mysql关于自增长增长问题
- Alibaba testers use UI automated testing to achieve element positioning
- How to modify field constraints (type, default, null, etc.) in a table
- 登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
- Record the process of reverse task manager
- 3.1 rtthread 串口设备(V1)详解
- 施努卡:什么是视觉定位系统 视觉系统如何定位
- Custom event of C (31)
- Indicator system of KQI and KPI
- Image super-resolution using deep convolutional networks(SRCNN)解读与实现
猜你喜欢
![[001] [stm32] how to download STM32 original factory data](/img/5a/02d87fe1409a9427180ecefb8326c6.jpg)
[001] [stm32] how to download STM32 original factory data

Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
![[optimization model] Monte Carlo method of optimization calculation](/img/e6/2865806ffbbfaa8cc07ebf625fcde6.jpg)
[optimization model] Monte Carlo method of optimization calculation

2.2 STM32 GPIO operation

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

How do we make money in agriculture, rural areas and farmers? 100% for reference

How to modify field constraints (type, default, null, etc.) in a table
![[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board](/img/07/973722bf484b374f752177dfc48ef5.png)
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board

2.2 fonctionnement stm32 GPIO

在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒
随机推荐
User experience index system
Blue style mall website footer code
RT-Thread--Lwip之FTP(2)
Pytoch foundation - (2) mathematical operation of tensor
Prime protocol announces cross chain interconnection applications on moonbeam
RT thread -- FTP of LwIP (2)
2.2 STM32 GPIO operation
On Data Mining
C form application of C (27)
Introduction to data types in MySQL
Schnuka: visual positioning system working principle of visual positioning system
P7735-[noi2021] heavy and heavy edges [tree chain dissection, line segment tree]
Overview of super-resolution reconstruction of remote sensing images
Canvas cut blocks game code
MySQL 中的数据类型介绍
Ybtoj coloring plan [tree chain dissection, segment tree, tarjan]
【Rust 笔记】18-宏
[optimization model] Monte Carlo method of optimization calculation
[rust notes] 18 macro
Pytorch基础——(2)张量(tensor)的数学运算