public class BuyTicket implements Runnable{
private int ticketNums=10;
@Override
public void run() {
for(int i=1;i<=ticketNums;i++){
if(ticketNums<0){
break;
}
System.out.println(Thread.currentThread().getName() + "抢到了第" + i + "张票");
ticketNums--;
}
}
public static void main(String[] args) {
BuyTicket buyTicket=new BuyTicket();
new Thread(buyTicket,"张三").start();
new Thread(buyTicket,"李四").start();
new Thread(buyTicket,"王五").start();
}
}
三个人同时抢票,总票数10张,抢完为止
三个进程同时进行