你问的是一个生产者 - 消费者模型
你可以阅读here
基本理念:网站是生产者,您作为客户是消费者。
你等待(),直到听众收到一条消息然后notifAll()
class WaitForAmazon{
private boolean available = false;
private int contents;
public synchronized int consumer() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) { }
}
available = false;
notifyAll();
return contents;
}
public synchronized void producer(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) { }
}
contents = value;
available = true;
notifyAll();
}
}