So, I have the following object (simplified for sake of example):
public class SomeListener implements EventListener{
public final Object lock = new Object();
public int receivedVal;
@Override
public onDataAvailable(int val){
synchronized(lock){
System.out.println("listener received val: " + val);
receivedVal = val;
lock.notifyAll();
}
}
}
And I have this piece of code somewhere in the main thread (again, simplified):
SomeListener listener = new SomeListener();
EventGenerator generatorThread = new EventGenerator();
generatorThread.addListener(listener);
synchronize(listener.lock){
generatorThread.start();
listener.lock.wait();
System.out.println("value is: " + listener.receivedVal);
}
//some other stuff here....
Now, the EventGenerator object calls “onDataAvailable” with val = 1, then with val = 2 on a different thread. Basically, what I expect to see is:
listener received val: 1
value is: 1
listener received val: 2
However, I usually get:
listener received val: 1
listener received val: 2
value is: 2
It is as if the second call of “onDataAvailable” acquires the lock before the main thread is awaken. A simple println or a short sleep after the synchronized block of “onDataAvailable” is enough to get the expected result, but that seems like an ugly patch.
What am I doing wrong here?
Note, I do not have control on the thread that calls the listener. It’s basically a thread that receives events over the network. Sometimes it will receive multiple events in the same message and will therefore call “onDataAvailable” multiple times one after the other, which leads to my problem. Other times it will receive two events in two different messages, which leaves enough time for the main thread to awake between the events.
This is to be expected if you have multiple threads calling
onDataAvailable(...). WhennotifyAll()is called, all of the threads that are waiting on that object are moved to the blocked queue but behind any threads already in the queue. They all have to wait to synchronize on thelockbefore continuing.Right, so multiple network handler threads are calling
onDataAvailable(...). The 2nd one is blocked on thesynchronized(lock)waiting for it. WhennotifyAll()is called, the other thread goes into the block queue as well but behind the other handler.I would be surprised that you are getting that output if there is only one handler thread. In that case, the notified thread should get the synchronize lock before the single thread handler can unlock, read another message, and lock again.
The problem is not with the way the threads are being handled but by the way you are handling the
receivedVal. You should process the value immediately in the handling thread or you will need to put it in some sort of synchronized queue (maybe aLinkedBlockingQueue) to be printed out by the main thread in order.If you use a
BlockingQueuethen the main queue just does aqueue.take()which causes it to wait for a result and the handler threads just do aqueue.put(...). You would not need to do thewait()ornotifyAll()calls yourself.Something like this would work: