/**
* 类说明 :开启多个窗口同时售票
* @author 郭莹棋
* @date 2018年10月26日
*/
public class Test5 {
public static void main(String[] args) {
Mythread m = new Mythread();
new Thread(m,"窗口1").start();
new Thread(m,"窗口2").start();
new Thread(m,"窗口3").start();
new Thread(m,"窗口4").start();
}
}
class Mythread implements Runnable {
private int tickes = 10;
//设置锁
Object lock = new Object();
public void run() {
while(true) {
synchronized(lock) {
if(tickes > 0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在发售第"+tickes--+"张票");
}
}
}
/*private synchronized void sale() {
if(tickes > 0) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在发售第"+tickes--+"张票");
}
}*/
}