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 6974047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:11:12+00:00 2026-05-27T17:11:12+00:00

I have a thread that updates it’s state from time to time and I

  • 0

I have a thread that updates it’s state from time to time and I want a second thread to be able to wait for the first thread to be done. Something like this:

Thread 1:
    while(true) {
        ...do something...
        foo.notifyAll()
        ...wait for some condition that might never happen...
        ...
    }

Thread 2:
    ...
    foo.wait();
    ...

Now this looks nice and all unless Thread 1’s notifyAll() runs before Thread 2’s wait(), in which case Thread 2 waits until Thread 1 notifies again (which might never happen).

My possible solutions:

a) I could use a CountDownLatch or a Future, but both have the problem that they inherently only run once. That is, in Thread 1’s while loop, I would need to create a new foo to wait for each time and Thread 2 would need to ask which foo to wait for. I have a bad feeling about simply writing

while(true) {
   foo = new FutureTask(); 
   ...
   foo.set(...);
   ...wait for a condition that might never be set...
   ...
}

as I fear that at foo = new FutureTask(), what happens when someone waited for the old foo (for “some reason”, set was not called, e.g. a bug in the exception handling)?

b) Or I could use a semaphore:

class Event {
   Semaphore sem;
   Event() { sem = new Semaphore(1); sem . }
   void signal() { sem.release(); }
   void reset() { sem.acquire(1); }
   void wait() { if (sem.tryAcquire(1)) { sem.release(); } }
}

But I fear that there is some race condition, if multiple threads are wait()ing for it while another one signal()s and reset()s.

Question:

Is there nothing in the Java API that resembles the Windows Event behaviour? Or, if you despise Windows, something like golang’s WaitGroup (i.e. a CountDownLatch that allows countUp())? Anything?

How to do it manually:

Thread 2 cannot simply wait because of spurious wakeup and in Java there is no way to know why Object.wait() returned. So I need a condition variable that stores whether the event is signalled or not. Thread 2:

synchronized(foo) {
    while(!condition) {
        foo.wait();
    }
}

And Thread 1 of course sets condition to true in a synchronized block. Thanks to weekens for the hint!

Is there an existing class that wraps that behaviour?

Or do I need to copy and paste the code all over?

  • 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-05-27T17:11:13+00:00Added an answer on May 27, 2026 at 5:11 pm

    It is standard practice to change some state when performing notifyAll and to check some state when performing wait().

    e.g.

    boolean ready = false;
    
    // thread 1
    synchronized(lock) {
        ready = true;
        lock.notifyAll();
    }
    
    
    // thread 2
    synchronized(lock) {
        while(!ready) 
            lock.wait();
    }
    

    With this approach, it doesn’t matter if thread 1 or thread 2 acquires the lock first.

    Some coding analysis tools will give you a warning if you use notify or wait without setting a value or checking a value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below I have a thread that updates a 'clock' label once per second. Once
I want to have a background thread that will check for updates on the
I have a thread that does the following: 1) Do some work 2) Wait
I have a thread that downloads some images from internet using different proxies. Sometimes
I have the following problem: My app has a thread that updates the game
I have a program (time lapse maker) which has two threads that updates a
I have a thread that gathers a list of URLs from a website and
I have a stopwatch running in a different thread, that updates the GUI thread
My scenario: I have a background thread that polls for changes and periodically updates
I have a method that updates records from the database, and I wonder if

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.