62,628
社区成员
发帖
与我相关
我的任务
分享
/**
* @Description
* 设计一个多线程程序如下:
* 设计一个火车售票模拟程序。 假如火车站要有100张火车票要卖出,
* 现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况
* 1、要求打印出每个售票点所卖出的票号
* 2、各售票点不能售出相同票号的火车票
* @author XPY
* @date 2016年7月4日下午5:40:40
*/
public class ThreadTest3 {
public static void main(String[] args) {
Shop shop = new Shop();
for (int i = 1; i <= 5; i++) {
new Thread(shop,"").start();
}
}
}
class Shop implements Runnable{
String name;
ticket t;
public Shop() {
t.total = 100;
t.count = 0;
}
public void run() {
while (t.total>0) {
synchronized (this) {
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"_____"+t.count);
t.total--;
t.count++;
}
}
}
}
class ticket {
int total;
int count;
}
我写的一个小例子 ,你可以看看就知道为什么了