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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:02:10+00:00 2026-05-28T17:02:10+00:00

Consider the following code :- public class UsingWait1{ public static void main(String… aaa){ CalculateSeries

  • 0

Consider the following code :-

public class UsingWait1{
public static void main(String... aaa){
    CalculateSeries r = new CalculateSeries();
    Thread t = new Thread(r);
    t.start();
    synchronized(r){
        try{
            r.wait();   //Here I am waiting on an object which is Runnable. So from its run method, it can notify me (from inside a synchronized block).
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }

    }
    System.out.println(r.total);
    try{
        Thread.sleep(1);
    } catch (InterruptedException e){
        System.out.println("Interrupted");
    }
    System.out.println(r.total);

}
}

class CalculateSeries implements Runnable{
int total;
public void run(){
synchronized(this){
    for(int i = 1; i <= 10000; i++){
        total += i;
    }
    notify();   // Line 1 .. Notify Exactly one of all the threads waiting on this instance of the class to wake up
}
}
}

Here I am waiting on CalculateSeries which is Runnable. So I can notify the waiting thread from the run() method of CalculateSeries.

But now, consider the following code where I am waiting on an object which is not Runnable.

public class WaitNotOnThread{
public static void main(String... aaa){
    NotRunnable nr = new NotRunnable();
    IAmRunnable r = new IAmRunnable(nr);
    new Thread(r).start();
    synchronized(nr){
        try{
            nr.wait();
        } catch(InterruptedException e){
            System.out.println("Wait interrupted");             
        }

        System.out.println("After being notified within synchronized");
    }
    System.out.println("After synchronized");
}
}

class IAmRunnable implements Runnable{
NotRunnable nr;
IAmRunnable(NotRunnable nr){
    this.nr = nr;
}
public void run(){
    synchronized(nr){
        try{
            Thread.sleep(1000);
        } catch(InterruptedException e){
            System.out.println("Sleeping Interrupted :( ");
        }
        notify();                  // Line 2
    }
}
}

class NotRunnable{
}

Here I get an IllegalMonitorStateException at Line 2. I am waiting on the same instance of the object (which is not Runnable) while calling both, wait() as well as notify(). Then what is the problem?

Can someone also give some scenarios where it would be useful to wait on an object which is not Runnable??

  • 1 1 Answer
  • 1 View
  • 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-28T17:02:11+00:00Added an answer on May 28, 2026 at 5:02 pm

    Wait need not be on Runnable. That is why notify() is on Object and not on Runnable. I guess that helps in all cases we want to avoid busy wait.

    The problem seems to be the synchronized() is on nr, and the notify is called on different object. Also synchronized should be on final variables.

    class IAmRunnable implements Runnable {
         final NotRunnable nr;
    
        IAmRunnable( final NotRunnable nr) {
            this.nr = nr;
        }
    
        public void run() {
            synchronized (nr) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Sleeping Interrupted :( ");
                }
                nr.notify();                  // Line 2
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Consider the following code public class sqldetails extends JFrame implements ActionListener{ static String username=null;
Consider the following code public static void method(String[] srgs){ try{ }catch(){ System.out.println(Hello World +
Consider the following code: public class ReadingTest { public void readAndPrint(String usingEncoding) throws Exception
Consider the following code : public class Main implements Vehicle, Car { public static
Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public

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.