package com.Bubbles.Demo1;
//初始并发问题
//多个线程问题同时操作同一个对象
public class Testgread4 implements Runnable{
//票数
private int pivarun = 10;
@Override
public void run() {
//买票
while (true) {
if (pivarun < -0) {
break;
}
//模拟延时
try {
Thread.sleep (200);
} catch (InterruptedException e) {
e.printStackTrace ();
}
System.out.println (Thread.currentThread ().getName () + "-->拿到了第" + pivarun-- + "票");
}
}
//创建对象
public static void main(String[] args) {
Testgread4 testgread4 = new Testgread4 ();
//写三个线程
new Thread (testgread4,"小明").start ();
new Thread (testgread4,"老师").start ();
new Thread (testgread4,"黄牛党").start ();
}
}
//发现问题:多个线程操作同一个资源的情况下,线程不安全紊乱。
初始并发问题
最新推荐文章于 2023-01-08 21:40:10 发布