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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:10:42+00:00 2026-05-21T10:10:42+00:00

I am trying to get notifyAll() and wait() to work for another piece of

  • 0

I am trying to get notifyAll() and wait() to work for another piece of code. So I created a new program that behaves similarly to the other program.

My program has my main thread create a separate thread that will run notifyAll() after 20 seconds, and my main thread will wait for 60 seconds. So there should be plenty of time for my main thread to call wait() before the separate thread calls notifyAll(). But the problem is that my main thread does not wake up from the notifyAll() and waits the full 60 seconds. Why is this not working?

My code looks like this:

new Thread(new Runnable(){
    public void run(){
        synchronized (this){
            try{
                this.wait(20000);
                System.out.println("Wait 20 seconds then notify");
                this.notifyAll();
            } catch (Exception e){}
        }
    }
}, "test Thread").start();
System.out.println("started thread");

boolean timeout = false;
System.out.println("Start Waiting");
synchronized (this){
    try{
        this.wait(60000);
        timeout = true;
    } catch(Exception e){
        System.out.println("Did not wait 60 seconds");
    }
}
if (timeout){
    System.out.println("Waited 60 Seconds");
}

And the output I get is:

started thread
Start Waiting
Wait 20 seconds then notify
Waited 60 Seconds
  • 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-21T10:10:42+00:00Added an answer on May 21, 2026 at 10:10 am

    wait and notify must use the same object reference in order to work. in this case, your notify is using the Runnable instance, and your wait is using the main class instance.

    you could fix it by synchronizing on “MainClass.this” in the Runnable instance (using whatever the name of the main class is).

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

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying get Struts 2 and Tiles to work and I am using
When I'm trying get method from remote webservice it gives me error. My code
Ok, I just bought the new 27 inch iMac and I am trying get
I'm trying get LocalConnection to work between two swf's placed within two different IFRAMES.
i am very new to regular expression and trying get \ character using python
I am new on Ruby and I am trying get data from table. so
I'm trying get some licensing code from AndroidPit.com working, but I get Unable to
I am trying get Javascript popup calendar control to work which doesnt work. I
I am trying get my hands dirty with async CTP and I noticed that

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.