package example;
public class TestAB {
public static void main(String[] args) {
new TestAB().test();
}
public void test() {
Object obj = new Object();
new Thread(new Printer(obj)).start();
new Thread(new Printer(obj)).start();
new Thread(new Printer(obj)).start();
new Thread(new Printer(obj)).start();
}
}
class Printer implements Runnable {
Object obj;
public Printer(Object obj) {
this.obj = obj;
}
@Override
public void run() {
String name = Thread.currentThread().getName();
for (int i = 0; i < 100; i++) {
synchronized (obj) {
System.err.println(name + ":" + i);
try {
obj.notifyAll();
if (i < 99) {
obj.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}