用多线程,实现如下功能,
用户去火车站取票,,有四个窗口,,票自己订,,,,实现一下,新手,,,求救,
package xatu.zsl.main;
/**
class MyThread implements Runnable {
//初始化有十张票,,设置为可见的整形,防止并发操作出错
volatile int ticketNum = 100;
Object object = new Object();
public void run() {
while (this.ticketNum > 0) {
synchronized (object) {//保证扣票和输出是一个整体
if (this.ticketNum > 0) { //防止卖出负票
//售出一张
this.ticketNum = this.ticketNum - 1;
System.out.println(Thread.currentThread().getName() + "售出一张火车票,还剩" + this.ticketNum + "张");
} else {
return;
}
}
}
}
}
package xatu.zsl.main;
/**
class MyThread implements Runnable {
//初始化有十张票,,设置为可见的整形,防止并发操作出错
volatile int ticketNum = 100;
Object object = new Object();
public void run() {
while (this.ticketNum > 0) {
synchronized (object) {//保证扣票和输出是一个整体
if (this.ticketNum > 0) { //防止卖出负票
//售出一张
this.ticketNum = this.ticketNum - 1;
System.out.println(Thread.currentThread().getName() + "售出一张火车票,还剩" + this.ticketNum + "张");
} else {
return;
}
}
}
}
}
哎,,,,这格式也是让人无语了,,,,仅供参考吧,,,,不懂随时追问