public void run() {
int i = 0;
while(true){
synchronized(this){
if(piao>0){
piao--;
i++;
System.err.println("剩余车票:"+piao+" "+"出售于"+Thread.currentThread().getName()+" "+"此线程共销售"+i+"张");}else{break;}}}}
int i = 0;
while(true){
synchronized(this){
if(piao>0){
piao--;
i++;
System.err.println("剩余车票:"+piao+" "+"出售于"+Thread.currentThread().getName()+" "+"此线程共销售"+i+"张");}else{break;}}}}
2017-06-10
synchronized的用法也不宜跟while(tickets>0)需要while(true)你们自己理解下,完整代码如下:
2017-06-10
MyThread mt = new MyThread("火车站窗口");
Thread mt1 = new Thread(mt,"窗口一");
Thread mt2 = new Thread(mt,"窗口二");
Thread mt3 = new Thread(mt,"窗口三");
mt1.start();
mt2.start();
mt3.start();
这样就不会多卖了
Thread mt1 = new Thread(mt,"窗口一");
Thread mt2 = new Thread(mt,"窗口二");
Thread mt3 = new Thread(mt,"窗口三");
mt1.start();
mt2.start();
mt3.start();
这样就不会多卖了
2017-03-21