public class Demo01_Tickets implements Runnable{
public static void main(String[] args) {
for (int tickets=100; tickets>0; tickets--) {
Demo01_Tickets mr = new Demo01_Tickets();
Thread th1 = new Thread(mr,"窗口a");
Thread th2 = new Thread(mr,"窗口b");
Thread th3 = new Thread(mr,"窗口c");
Thread th4 = new Thread(mr,"窗口d");
th1.start();
th2.start();
th3.start();
th4.start();
}
}
private int tickets = 0;
@Override
public void run() {
synchronized (this) {
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("当前窗口为:"+Thread.currentThread().getName()
+ ",剩余票数为" + tickets--);
}
}
|
|