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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:41:47+00:00 2026-05-27T20:41:47+00:00

import java.util.*; import java.io.*; import java.util.regex.*; class ZiggyTest2 extends Thread{ String sa; public ZiggyTest2(String

  • 0
import java.util.*;
import java.io.*;
import java.util.regex.*;

class ZiggyTest2 extends Thread{

    String sa;

    public ZiggyTest2(String sa){
        this.sa = sa;
    }

    public void run(){
        synchronized(sa){
            while(!sa.equals("Done")){
                try{
                    sa.wait();
                }catch(InterruptedException is){System.out.println("IE Exception");}
            }
        }

        System.out.println(sa);
    }
}

class Test{
    private static String sa = new String("Not Done");

    public static void main(String[] args){
        Thread t1 = new ZiggyTest2(sa);
        t1.start();

        synchronized(sa){
            sa = new String("Done");
            sa.notify();
        }
    }
}

When i run the above program i get the following exception:

Exception in thread "main" java.lang.IllegalMonitorStateException
        at java.lang.Object.notify(Native Method)
        at Test.main(ZiggyTest2.java:35)

A couple of questions:

  • Why the IllegalMonitorStateException? Because Test.sa is assigned to a new String object, i was expecting the ZiggyTest2 thread to wait indefinately because sa.notify() will be called on a different lock than the one used in ZiggyTest2.

  • In the above example, wait() & notify() are called on the “sa” object. What is the difference in say calling notify() on its own and calling notify()/wait() using an object i.e. sa.wait() and sa.notify()?

  • Does it matter that in the Test class the synchronised block has the lock for the sa object and the sa object is static but in the ZiggyTest2 class, the synchronised block uses the same sa object reference but using a non static reference? Given that one is static and the other is not, will they both still be using the same lock?

  • 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-27T20:41:48+00:00Added an answer on May 27, 2026 at 8:41 pm

    When you execute

    sa = new String("Done");
    

    you don’t change the content of the String references by sa. You assign a new String instance (a new object) to sa. Strings are immutable. It’s impossible to change their value.

    This means that you synchronize on sa (first object : “Not Done”), then assign a new object to sa (second object : “Done”), and call notify on this second object. Since you haven’t synchronized on the second object, but on the first one, you get an IllegalMonitorException. Calling notify on an object is only allowed if you own he intrinsic lock on the object. That’s why a lock should always be final.

    Calling notify() is equivalent to calling this.notify(). So this.notify() and sa.notify() are just calling notify() on two different objects. The first one will notify a thread waiting on this, and the second one will notify a thread waiting on sa.

    The fact that a variable is static or not does not matter. The lock is associated to the object, not to its reference.

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

Sidebar

Related Questions

import java.util.regex.*; import java.io.*; class Patmatch{ static String str = ; public static void
Java Code: import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegExpTest { public static void main(String[]
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
Consider this code: import java.util.regex.*; public class Pattern3 { /** * @param args the
import java.util.scanner; import javax.swing.JOptionPane; public class FirstHomeJavaApplet{ public static void main(String[] args){ int num1=2;
import java.util.*; public class Test { public static void main(String[] args) { Map<String,String> map
import java.util.Scanner; class Practice { public static void main(String args[]) { System.out.println(Enter the number
I am really struggling with this question: import java.util.regex.*; class Regex2 { public static
import java.util.Collection; public class Test { public static void main(String[] args) { Collection c

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.