//使用继承的这种方式实现多线程,加了锁了但是还是出现了同一张票被多个窗口卖的情况呀,何解????代码只提供Mythread类
package cn.itcast3;
class MyThread extends Thread {
// 构造方法
MyThread() {
}
MyThread(String name) {
super(name);
}
private static int num = 10;
Object obj = new Object();
public void run() {
while (true) {
synchronized (obj) { // 加锁了,一次还是卖三张?????????????????????
if (num > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// e.printStackTrace();
}
System.out.println(getName() + "正在出售" + (num--) + "张票");
} else {
System.out.println("没票了!");
break;
}
}
}
}
}
|
|