-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
public class PrintRunable implements Runnable {
private String name;
private Object prev;
private Object self;
private PrintRunable(String name, Object prev, Object self) {
this.name = name;
this.prev = prev;
this.self = self;
}
@Override
public void run() {
int count = 10;
while (count > 0) {
synchronized (prev) {
synchronized (self) {
System.out.print(name);
count--;
try{
Thread.sleep(1);
}
catch (InterruptedException e){
e.printStackTrace();
}
self.notify();
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
Object a = new Object();
Object b = new Object();
Object c = new Object();
PrintRunable pa = new PrintRunable("A", c, a);
PrintRunable pb = new PrintRunable("B", a, b);
PrintRunable pc = new PrintRunable("C", b, c);
new Thread(pa).start();
Thread.sleep(100);
new Thread(pb).start();
Thread.sleep(100);
new Thread(pc).start();
Thread.sleep(100);
}
}Metadata
Metadata
Assignees
Labels
No labels