当前位置:网站首页>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
边栏推荐
- Definition of Halcon hand eye calibration
- Younger sister Juan takes you to learn JDBC -- two days' Sprint Day2
- Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
- Old love letters
- 机器学习 07:PCA 及其 sklearn 源码解读
- Factory method pattern, abstract factory pattern
- What management systems (Updates) for things like this
- 05. basic data type - Dict
- Status mode, body can change at will
- NPM private server problem of peanut shell intranet penetration mapping
猜你喜欢

小程序如何关联微信小程序二维码,实现二码聚合

pytorch(网络模型训练)

423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)

Ribbon load balancing service call

状态模式,身随心变

String class learning

Overloading and overriding

MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

REUSE_ ALV_ GRID_ Display event implementation (data_changed)

【群内问题学期汇总】初学者的部分参考问题
随机推荐
5 minutes to learn regular expressions
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
新的征程
Kolla ansible deploy openstack Yoga version
Func < T, tresult > Commission - learning record
BOM document
Selective search for object recognition paper notes [image object segmentation]
【 langage c】 stockage des données d'analyse approfondie en mémoire
pytorch(网络模型训练)
numpy.tile()
numpy. tile()
Summary of JVM interview focus (II) -- garbage collector (GC) and memory allocation strategy
String class learning
06. talk about the difference and coding between -is and = = again
Bubble sort
一段不离不弃的爱情
kolla-ansible部署openstack yoga版本
Machine learning 05: nonlinear support vector machines
421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)
机器学习 05:非线性支持向量机