R
Richard Wilson
Can anyone shed any light on why the wait() method doesn't recognise
that a notify() has occured before it's called?
I have a simple thread for a message queue running a loop like:
public synchronized void addItem(Object item) {
list.addElement(item);
notify();
}
public synchronized void stop() {
running = false;
notify();
}
public void run() {
while (running) {
synchronized(this) {
if (list.size() != 0) {
copy the items
list.removeAllElements();
}
}
processItems();
synchronized(this) {
wait();
}
}
}
This keeps the synchronization blocks to a minimum, and means
processItems won't block the addItem method for long periods. The
problem is that if the addItem (or stop) method gets called while
the thread is in processItems, then the wait() call doesn't get
notified. I know it's easy enough to work around this using
another if just before the wait, but why on earth was
the wait/notify mechanism designed this way?
Richard
that a notify() has occured before it's called?
I have a simple thread for a message queue running a loop like:
public synchronized void addItem(Object item) {
list.addElement(item);
notify();
}
public synchronized void stop() {
running = false;
notify();
}
public void run() {
while (running) {
synchronized(this) {
if (list.size() != 0) {
copy the items
list.removeAllElements();
}
}
processItems();
synchronized(this) {
wait();
}
}
}
This keeps the synchronization blocks to a minimum, and means
processItems won't block the addItem method for long periods. The
problem is that if the addItem (or stop) method gets called while
the thread is in processItems, then the wait() call doesn't get
notified. I know it's easy enough to work around this using
another if just before the wait, but why on earth was
the wait/notify mechanism designed this way?
Richard