package com.hhr.cn.tread;
public class ThreadDemo4 {
public static void main(String[] args) {
SellCard01 sell=new SellCard01();
new Thread(sell,"窗口1").start();
new Thread(sell,"窗口2").start();
new Thread(sell,"窗口3").start();
new Thread(sell,"窗口4").start();
}
}
package com.hhr.cn.tread;
//模拟火车张卖票,,,四个窗口同时卖
public class SellCard01 implements Runnable {
private static int count=20;
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
//启用同步方法时 这边默认的对象是 this
if(check()) return;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized boolean check(){
if(count>0){
count--;
System.out.println(Thread.currentThread().getName()+"剩余"+count+"张票!!");
return false;
}else{
System.out.println("票售完");
return true;
}
}
}
package com.hhr.cn.tread;public class ThreadDemo4 { public static void main(String[] args) { SellCard01 sell=new SellCard01(); new Thread(sell,"窗口1").start(); new Thread(sell,"窗口2").start();...