我想知道例如 r.wait() 是否有效?使用此代码:
public class Buffer1<T> {
private T content;
private boolean empty;
private Object r = new Object();
private Object w = new Object();
public Buffer1() {
empty = true; }
public Buffer1(T content) {
this.content = content;
empty = false; }
public T take() throws InterruptedException {
synchronized (r) {
while (empty) {
r.wait();
}
synchronized (w) {
empty = true;
w.notify();
return content;
}
}
}
public void put(T o) throws InterruptedException {
synchronized(w) {
while (!empty) {
w.wait();
}
synchronized (r) {
empty = false;
r.notify();
content = o;
}
r.wait()、w.wait()、r.notify()、w.notify() 是如何工作的?它们如何与同步(r)/同步(w)一起工作?