先上代码
public class MyThread implements Runnable {
private String name;
private String prev;
private String self;
public MyThread(String name, String prev, String 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.println(name);
self.notify();
count--;
}
try {
prev.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
String a = new String("a");
String b = new String("b&#