//定义类,实现Runnable接口,重写run方法
class TicketRunnable implements Runnable{
private int tickets = 100;
private Object obj = new Object();
public void run(){
while(true){
synchronized(obj){
if(tickets>0){
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"出售第"+tickets--);
}
}
}
}
}
public class ThreadDemo8 {
public static void main(String[] args) {
//创建Thread类对象,传递Runnable接口的实现类的对象
TicketRunnable t = new TicketRunnable();
Thread t0 = new Thread(t);
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);