设计三个线程,实现动物园售票窗口进行卖票,每天总共最多只能出售100张票,售完后所有窗口都停止售票

匿名 (未验证) 提交于 2019-12-03 00:39:02
package com.multith.java;  public class Ticket implements Runnable{  	private int num=100; 	@Override 	public void run() { 		 			while(true) { 				synchronized (this) {  //同步代码块 					if(num>0) { 						try { 							Thread.sleep(100); 						} catch (InterruptedException e) { 							// TODO Auto-generated catch block 							e.printStackTrace(); 						} 						num--; 						System.out.println(Thread.currentThread().getName()+  								" 卖出一张票,还剩:"+ num + "张"); 					}else { 						System.out.println(Thread.currentThread().getName()+" 票已售完!"); 						break; 					} 			} 			 		} 	} 	 	public static void main(String[] args) { 		Ticket tic = new Ticket(); 		 		Thread t1 = new Thread(tic,"窗口A"); 		Thread t2 = new Thread(tic,"窗口B"); 		Thread t3 = new Thread(tic,"窗口C"); 		 		t1.start(); 		t2.start(); 		t3.start(); 		 	}  }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!