当前位置:网站首页>Getting to know concurrency problems
Getting to know concurrency problems
2022-06-26 05:59:00 【Mr.Rop】
Initial understanding of concurrency problem
// Multiple threads operate on the same object at the same time
// An example of buying a train ticket
// Find the problem : When multiple threads operate on the same resource , Thread unsafe , Data disorder
public class TestThread4 implements Runnable {
private int ticketNum = 10;
@Override
public void run() {
while (true){
if (ticketNum <= 0){
break;
}
// Analog delay
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "---> Got the number " + ticketNum-- + " ticket ");
}
}
public static void main(String[] args) {
TestThread4 testThread4 = new TestThread4();
new Thread(testThread4," Xiao Ming ").start();
new Thread(testThread4," Xiaohong ").start();
new Thread(testThread4," Cattle ").start();
}
}
give the result as follows
Cattle ---> Got the number 9 ticket
Xiao Ming ---> Got the number 8 ticket
Xiaohong ---> Got the number 10 ticket
Xiaohong ---> Got the number 7 ticket
Xiao Ming ---> Got the number 5 ticket
Cattle ---> Got the number 6 ticket
Cattle ---> Got the number 4 ticket
Xiao Ming ---> Got the number 4 ticket
Xiaohong ---> Got the number 4 ticket
Cattle ---> Got the number 3 ticket
Xiao Ming ---> Got the number 3 ticket
Xiaohong ---> Got the number 2 ticket
Cattle ---> Got the number 1 ticket
Xiaohong ---> Got the number 1 ticket
Xiao Ming ---> Got the number 1 ticket
边栏推荐
- numpy. random. choice
- Pytorch (network model)
- Sql查询时间段内容
- About XXX management system (version C)
- 【 langage c】 stockage des données d'analyse approfondie en mémoire
- The difference between abstract and interface interface
- MEF framework learning record
- Class and object learning
- Gram matrix
- Definition of Halcon hand eye calibration
猜你喜欢

Kolla ansible deploy openstack Yoga version
![[C language] deep analysis of data storage in memory](/img/2e/ff0b5326d796b9436f4a10c10cfe22.png)
[C language] deep analysis of data storage in memory

【 langage c】 stockage des données d'analyse approfondie en mémoire

Vs2022 offline installation package download and activation

423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)

DOM document

Cython入门

Bubble sort
![[intra group questions semester summary] some reference questions for beginners](/img/39/ba5b7ce3ab86433f29c9fa3ced4ddd.jpg)
[intra group questions semester summary] some reference questions for beginners

电商借助小程序技术发力寻找增长突破口
随机推荐
解决在win10下cmder无法使用find命令
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
Cython入门
SQL server functions
虚拟项目失败感想
The news of thunderbolt
bingc(继承)
How Navicat reuses the current connection information to another computer
COW读写复制机制在Linux,Redis ,文件系统中的应用
Sql查询时间段内容
Prototype mode, Baa Baa
状态模式,身随心变
Posting - don't get lost in the ocean of Technology
NPM private server problem of peanut shell intranet penetration mapping
机器学习 05:非线性支持向量机
Day4 branch and loop
Detailed explanation of serial port communication principle 232, 422, 485
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
循环位移
The use of loops in SQL syntax