锁对象的小问题
class Printer implements Runnable {
private static Object lock = new Object();//把此处的static去掉,为什么程序就死锁了???
private static int current = 0;
private String content = null;
private int flag = 0;
public Printer(String content, int flag) {
this.content = content;
this.flag = flag;
}
public void run() {
int times = 0;
while (times < 19) {
synchronized (lock) {
while (current % 3 != flag) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(content);
times++;
Printer.current++;
lock.notifyAll();
}
}
}
}
public class ThreadTest {
public static void main(String[] args) {
new Thread(new Printer("A", 0)).start();
new Thread(new Printer("B", 1)).start();
new Thread(new Printer("C", 2)).start();
}
}