简体   繁体   中英

Java notifyAll not waking right away

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.

It is as if the second call of "onDataAvailable" acquires the lock before the main thread is awaken

This is to be expected if you have multiple threads calling onDataAvailable(...) . When notifyAll() 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 the lock before continuing.

Other times it will receive two events in two different messages, which leaves enough time for the main thread to awake between the events.

Right, so multiple network handler threads are calling onDataAvailable(...) . The 2nd one is blocked on the synchronized(lock) waiting for it. When notifyAll() 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.

What am I doing wrong here?

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 a LinkedBlockingQueue ) to be printed out by the main thread in order.

If you use a BlockingQueue then the main queue just does a queue.take() which causes it to wait for a result and the handler threads just do a queue.put(...) . You would not need to do the wait() or notifyAll() calls yourself.

Something like this would work:

private final BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
...

@Override
public onDataAvailable(int val){
    System.out.println("listener received val: " + val);
    queue.put(val);
}
...

generatorThread.addListener(listener);
generatorThread.start();
while (true) {
    // this waits for the queue to get a value
    int val = queue.take();
    System.out.println("value is: " + val);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM