public class MyThread extends Thread{ private static int ticket=100; public void run(){ for(int i=0;i<50;i++) if(ticket>0) System.out.println(Thread.currentThread().getName()+"正在卖第"+(ticket--)+"张票"); } //main函数是一个主线程 public static void main(String[] args) { MyThread mt=new MyThread(); Thread m1=new Thread(mt,"window1"); Thread m2=new Thread(mt,"window2"); Thread m3=new Thread(mt,"window3"); m1.start(); m2.start(); m3.start(); } }
模仿火车票售卖,不同的窗口售卖同一种票,不能出现两个窗口卖出同一张票。
时间: 2024-10-06 09:53:59