Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:33:08+00:00 2026-06-10T20:33:08+00:00

So, I have the following object (simplified for sake of example): public class SomeListener

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:33:09+00:00Added an answer on June 10, 2026 at 8:33 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simplified ViewModel public class UserViewModel : IUserViewModel { public DelegateCommand<object>
If I have created the following Employee object (simplified)... public class Employee { public
I have the following object: public class QueueItem { public long _id { get;
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer
I have the following (obviously simplified) class class A(object) def __init__(self, a): self.a =
I have the following setup (simplified, obviously): An abstract class, with an A object
I have the following helper class (simplified): public static class Cache { private static
I have the following simplified class I'm mocking: class myClass(object): @staticmethod def A(): #...
Say for example I have the following type: public class Site { public string
Say you have the following object: public class Address { public String Line1 {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.