火车站总共有100张票, 四个窗口同时卖票, 当票卖光了之后,提示"没票了...",

public class Train{
	static int num = 100;
	public class Train {
	static int num = 100;
	public static void main(String[] args) {
		Runnable r = new Runnable() {
			public void run() {
				while(true) {
					synchronized (args) {
						try {
                                                        //方便查看效果
							Thread.sleep(200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						if (num>0) {
							
						//提示第几号窗口买票	System.out.println(Thread.currentThread().getName()+"买了一张票");
							num--;
							System.out.println("还剩余"+num+"张票");
						}else {
							System.out.println(Thread.currentThread().getName()+"没票了...");
							break;
						}
					}
					
				}
				
			}
		};
		Thread w1 = new Thread(r, "一号窗口");
		Thread w2 = new Thread(r, "二号窗口");
		Thread w3 = new Thread(r, "三号窗口");
		Thread w4 = new Thread(r, "四号窗口");
		w1.start();
		w2.start();
		w3.start();
		w4.start();
	}
}

多线程之火车站多窗口买票

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
  • 2021-11-29
  • 2021-12-18
  • 2021-08-29
  • 2021-04-16
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-15
  • 2021-12-09
  • 2021-12-30
  • 2022-12-23
  • 2022-02-05
  • 2022-01-05
相关资源
相似解决方案