要求:四个线程,分别命名为1号窗口、2号窗口... 一共卖10张票,每个窗口买票的数量大致相同,每张票唯一。
public class ThreadWork1 implements Runnable{
private int ticket = 10;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (ticket > 0){
System.out.println(Thread.currentThread().getName() + "----正在卖第" + (ticket--) + "张票");
}
}
}
public static void main(String[] args) {
//创建4个线程
ThreadWork1 r = new ThreadWork1();
Thread t1 = new Thread(r,"1号窗口");
Thread t2 = new Thread(r,"2号窗口");
Thread t3 = new Thread(r,"3号窗口");
Thread t4 = new Thread(r,"4号窗口");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
效果图