package syn;
public class UnSafeBuyTicket {
public static void main(String []args){
BuyTicket station = new BuyTicket();
new Thread(station,"苦逼的我").start();
new Thread(station,"牛逼的你们").start();
new Thread(station,"可恶的黄牛党").start();
}
}
class BuyTicket implements Runnable{
private int ticketNums = 10;
boolean flag = true;
public void run(){
while(flag){
try {
buy();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
private synchronized void buy() throws InterruptedException {
if(ticketNums<=0){
flag = false;
return;
}
System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);
}
}
04-01
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-20
1167
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-17
382
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交