实验内容:
1.哈尔滨火车站下面有三个火车票代售点,假如哈尔滨到北京的火车票总共是200张,如何用程序来实现三个售票点同时卖票的功能。
2.设计5个人排队买票,并规定买票规则和排队顺序。
代码实现:
package cn.edu.nefu.zhihu.xiancheng;
public class ChuangKou implements Runnable{
int m=200;
public void run() {
while(m>0){
synchronized(this){
if(m>0)
System.out.println(Thread.currentThread().getName()+"窗口卖出一张后,"+"总票数还剩票数: "+(--m));
try{
Thread.sleep(10);
}
catch(InterruptedException e){}
}
}
}
}
package cn.edu.nefu.zhihu.xiancheng;
public class Test {
public static void main(String[] args) {
ChuangKou ck=new ChuangKou();
Thread t1=new Thread(ck,"ck1");
Thread t2=new Thread(ck,"ck2");
Thread t3=new Thread(ck,"ck3");
t1.start();
t2.start();
t3.start();
while(true){
if(ck.m==0){
System.o