package com.zy.demo03;
public class TrainRun implements Runnable{
static int num = 20;//火车票
static Integer in = new Integer(1);//定义静态对象
@Override
public void run() {
while (true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//同步锁
synchronized (in){
if(num>0){
System.out.println("还有"+num+"张票");
num--;
}else{
System.out.println("已售完");
System.exit(0);
}
}
}
}
public static void main(String[] args) {
//开三个线程同时售票
Thread t = new Thread(new TrainRun());
t.start();
Thread t2 = new Thread(new TrainRun());
t2.start();
Thread t3 = new Thread(new TrainRun());
t3.start();
}
}
三个窗口同时卖票 用 实现Runnable方式
最新推荐文章于 2023-02-18 17:58:10 发布