public static class ThreadPrinter implements Runnable {
private String name;
private Object prev;
private Object self;
private ThreadPrinter(String name, Object prev, Object self) {
this.name = name;
this.prev = prev;
this.self = self;
}
@Override
public void run() {
int count = 3; //Print times
while (count > 0) {//Multi-threaded concurrency, cannot use if, must use loop to test waiting conditions to avoid false wakeup synchronized
(prev) { // Get the prev lock first
synchronized (self) {// Then get the self lock
System.out.print(name);
count--;
self.notifyAll();// release self first, wake up other threads to compete for self lock
}
try {
prev.wait(); // release prev again, sleep and wait for wakeup
/ **
* Note that after notify() is called, the object lock is not released immediately, but the lock is automatically released after the execution of the corresponding synchronized(){} statement block. * JVM will wait() the thread of the
object lock Randomly select a thread, give it an object lock, wake up the thread, and continue execution.
*/
} 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();
ThreadPrinter pa = new ThreadPrinter("A", c, a);
ThreadPrinter pb = new ThreadPrinter("B", a, b);
ThreadPrinter pc = new ThreadPrinter("C", b, c);
new Thread(pa).start();
// Thread.sleep(3);
new Thread(pb).start();
// Thread.sleep(10);
new Thread(pc).start();
// Thread.sleep(10);
}
private String name;
private Object prev;
private Object self;
private ThreadPrinter(String name, Object prev, Object self) {
this.name = name;
this.prev = prev;
this.self = self;
}
@Override
public void run() {
int count = 3; //Print times
while (count > 0) {//Multi-threaded concurrency, cannot use if, must use loop to test waiting conditions to avoid false wakeup synchronized
(prev) { // Get the prev lock first
synchronized (self) {// Then get the self lock
System.out.print(name);
count--;
self.notifyAll();// release self first, wake up other threads to compete for self lock
}
try {
prev.wait(); // release prev again, sleep and wait for wakeup
/ **
* Note that after notify() is called, the object lock is not released immediately, but the lock is automatically released after the execution of the corresponding synchronized(){} statement block. * JVM will wait() the thread of the
object lock Randomly select a thread, give it an object lock, wake up the thread, and continue execution.
*/
} 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();
ThreadPrinter pa = new ThreadPrinter("A", c, a);
ThreadPrinter pb = new ThreadPrinter("B", a, b);
ThreadPrinter pc = new ThreadPrinter("C", b, c);
new Thread(pa).start();
// Thread.sleep(3);
new Thread(pb).start();
// Thread.sleep(10);
new Thread(pc).start();
// Thread.sleep(10);
}