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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:43:56+00:00 2026-05-30T23:43:56+00:00

In my java program, when I code synchronized(a){ a = 55; a.wait(); } It

  • 0

In my java program, when I code

synchronized(a){
    a = 55;
    a.wait();
}

It gives run time exception java.lang.IllegalMonitorStateException on a.wait(); statement. However a=55 runs successfully.

But if I code as

synchronized(a){
    a.wait();
    a = 55;
}

then it runs perfectly without any exception. why so?

  • 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-30T23:43:57+00:00Added an answer on May 30, 2026 at 11:43 pm

    Because a is pointing to a different object after reassignment, and is not being synchronized on.

    In other words, there are two objects, val1 and val2 that are both assigned to the variable a. You synchronize on val1 but in the first example call wait on val2. The monitor you use is attached to the object, not its variable.

    You should thus avoid synchronizing by referencing a non-final variable. This leads to your confusion. If the field is mutable, use another lock, for example:

    Object aMonitor = new Object();
    
    synchronized(aMonitor) {
       a = 55;
       aMonitor.wait();
    }
    

    Ideally your scenario is just for learning though. wait() and notify() are primitives and shouldn’t be used unless as an exercise or for building your own concurrency library. If it’s for real code, use a higher level mechanism in java.util.concurrent.

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

Sidebar

Related Questions

I'm developing a multi-threaded Java program use different assertions throughout the code and run
I am writing a multithreaded program in which i am getting exception java.lang.IllegalThreadStateException .
I have the below code to run a java program from a file. import
Is it possible to make a Java program that prints its source code to
Our main program is in java but the code that extracts our data from
We have a Java program run as root on Unix, that therefore can read
I have a Java program with code: public class Test1 { public static void
I am writing a Java program that creates HTML code. What's the best/easiest possibility
I am throwing an exit code from a java program with System.exit(int). Should I
I am trying to port some code from a regular Java program into the

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.