第九周作业
完成火车站售票程序的模拟。要求:
(1)总票数1000张;
(2)10个窗口同时开始卖票;
(3)卖票过程延时1秒钟;
(4)不能出现一票多卖或卖出负数号票的情况。
一:实验代码packagedemo;publicclassMyThreadimplementsRunnable{privateintticket=25;publicvoidrun(){for(inti=0;i<25;i++) {synchronized(this) {if(ticket>0) {try{ Thread.sleep(1000); }catch(Exception e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"运行,ticket="+ticket--); } } } }};packagedemo;publicclasstest{publicstaticvoidmain(String[] args){ MyThread mt=newMyThread(); Thread M1=newThread(mt,"A窗口"); Thread M2=newThread(mt,"B窗口"); ThreadM3=newThread(mt,"C窗口"); Thread M4=newThread(mt,"D窗口"); ThreadM5=newThread(mt,"E窗口"); Thread M6=newThread(mt,"F窗口"); Thread M7=newThread(mt,"H窗口"); Thread M8=newThread(mt,"I窗口"); Thread M9=newThread(mt,"J窗口"); Thread M0=newThread(mt,"K窗口"); M1.start(); M2.start(); M3.start(); M4.start(); M5.start(); M6.start(); M7.start(); M8.start(); M9.start(); M0.start(); }}二:运行截图