package xiao;
public class XXiao {
public static void main(String[] args) {
// TODO 自动生成的方法存根
ThreadSellTciket sTicket=new ThreadSellTciket(); //创建属于类ThreadSellTciket的对象sTicket。
new Thread(sTicket).start(); //创建3个同优先级的线程。
new Thread(sTicket).start();
new Thread(sTicket).start();
}
}
class ThreadSellTciket implements Runnable{ //创建类ThreadSellTciket,实现接口Runnable。
private int tickets=10;
boolean flag=true;
public void run(){ //重写方法run(),在方法run()中调用方法sale()。
while(flag){
sale();
}
}
synchronized public void sale(){ //创建同步方法sale()。
if(tickets>0){ //各个售票线程对10张车票共同进行售票。
try{
Thread.sleep(100);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+”正在卖票:”+tickets);
tickets–;
}
else{