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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:48:41+00:00 2026-05-15T03:48:41+00:00

I have about 4 threads. One thread keeps checking some data that the other

  • 0

I have about 4 threads. One thread keeps checking some data that the other thread is updating. The others are doing some processing in the background. All have been started at this point.

My question is when the checking thread sees that the data has not been updated yet I currently sleep for a little bit but is there any way for me to tell the system to back to executing the thread that does the updating?

That or is there any way I can put something like a listener on the data(a String) and once its updated an event will fire that will do what it needs to do?

I tried using yield() and it seemed to just keep returning to the thread I called yield() from.

Thanks

  • 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-15T03:48:41+00:00Added an answer on May 15, 2026 at 3:48 am

    This kind of simple notification is what Object.wait() and Object.notify() are intended for.

    In your updater thread, you have

    void updateData() {
        synchronized (theData) {
           theData.doSomeUpdate();
           theData.notifyAll(); // tell other threads of a change
        }
    }
    

    And then in your checking thread, have

    void waitForUpdates() {
        synchronized (theData) {
           while (notCancelled) {
              theData.wait();  // wait for notification
              handleUpdate(theData);
           }
        }        
    }
    

    Don’t use Thread.sleep() since you can’t really wake up the thread, unless you interrupt it, and that’s a little nasty. Instead, use the wait/notify process above.

    You can also look at passing notifications via an explicit BlockingQueue that is shared between the threads. The updater thread puts events in the queue, and the checker thread uses take() to fetch update events from the queue, waiting if there are no updates in the queue.

    The difference with this scheme is that the updater thread can pass specific information about what has changed, rather than just saying “something changed”, as is the case with wait/notify.

    Also, the thread is notified of each update explicitly, so no updates are missed. It’s also more flexible than wait/notify, since notification of updates does not require a lock on the data.

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

Sidebar

Related Questions

I posted a thread about this earlier and have made some progress but now
I have some question about using queues and threads. I already made researches, but
I have some basic questions about core data (which I am new to) and
I have a thread that needs to write data from an in-memory buffer to
I have a Qt application that launches two threads from the main thread at
I have recently started studying about threads. I thought of starting from the beginning
I have read all threads about this without finding any answer to my problem.
So far I have learned about generating thread dump and heap dump using jstack
After following the great advice given in a thread about service beans I have
I have a question about the 'Event Dispatch Thread'. I have a Main class

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.