What you are asking is called the producer-consumer model. You can read about it here.
The main idea: the site is a manufacturer, and you are a customer customer.
you wait () until the listener receives a message, and then 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();
}
}
source
share