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

The Archive Base Latest Questions

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

So I have a java object that executes wait() and then another thread executes

  • 0

So I have a java object that executes wait() and then another thread executes notifyAll() on the same object.

When I initially tried this, it wouldn’t work. It would throw an IllegalMonitorStateException.

Thread refreshThread = new Thread(new Runnable() {

   final Object dirtyLock = new Object();

   public void run() {

      while(true)
      {     
         try
         {
            dirtyLock.wait();
         } 
         catch (InterruptedException e)
         {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
});
refreshThread.start();

But if i surround the while(true) loop with the following code, it works.

synchronized(dirtyLock){ /* while loop */ }

Now I had to do this in the function that notifies this thread to awake too.

synchronized(dirtyLock){ dirtyLock.notifyAll(); }

Does synchronized give the thread ownership of the monitor object? How can two different threads claim ownership of the same monitor?

p.s. the question may sound dumb to some but I just want to learn how this behaviour works instead of just blindly coding.

  • 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-27T06:12:57+00:00Added an answer on May 27, 2026 at 6:12 am

    You can’t wait() until you’re holding the monitor.

    For “old-school” Java concurrency (with the synchronized keyword as the main tool), then it works like this:

    The synchronized(obj) { … } construct only lets the thread which holds the monitor belonging to obj inside the block (or method) that’s tagged with “synchronized”. Only 1 thread can hold the obj’s monitor at a time.

    So, when threads try to enter the synchronized block (or method), only 1 will succeed. Any other threads which are trying must not enter the block.

    So, by definition, when you’re proceeding through a synchronized block, you are the thread that’s holding the monitor of obj.

    When you hit wait() then you have to go to sleep, and as you do so, you let go of the monitor. Other threads can now try to acquire it.

    By the way, I can see what you’re trying to do with the dirtyLock variable.

    It already exists in Java – it’s in a really awesome concurrency toolbox called java.util.concurrent (which ships with the JDK) – it’s one of the real highlights of the platform and you should definitely use it, instead of trying to code this up from scratch. You can read loads about it online, or buy a book called “Java Concurrency in Practice” by Brian Goetz & co.

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

Sidebar

Related Questions

*I'm using Java. I have this thread, agent, that explores a room to determine
I have a Java Properties object that I load from an in-memory String ,
If you have a java.io.InputStream object, how should you process that object and produce
I have a java.sql.ResultSet object containg data from a query that was run. How
I have a hibernate object that gets detached and transferred to a thick java
Suppose I have a Java method that returns a HashMap object. Because a LinkedHashMap
I have noticed in Java that you can have a function with object... as
I have a C program that stores some object in java store using JNI.
I have a Graphics object of JPanel and that is working fine: import java.awt.Color;
I have a java.sql.Clob object which I populate from an Oracle query and then

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.