62,614
社区成员
发帖
与我相关
我的任务
分享
class SaleTickets2 extends Thread{
static int num = 50;//票数
public SaleTickets2(String name) {
super(name);
}
@Override
public void run() {
while(true){
if(num>0){
System.out.println(Thread.currentThread().getName()+"售出了第"+num+"号票");
num--;
}else{
System.out.println("售罄了..");
break;
}
}
}
}
public class Demo4_2 {
public static void main(String[] args) {
//创建线程对象
SaleTickets2 thread1 = new SaleTickets2("窗口1");
SaleTickets2 thread2 = new SaleTickets2("窗口2");
SaleTickets2 thread3 = new SaleTickets2("窗口3");
//开启线程
thread1.start();
thread2.start();
thread3.start();
//System.out.println("test");
}
}