• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions

 
Greenhorn
Posts: 18
Android Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everyone,
m preparing for SCJP 1.6.I have a doute in the following code could anyone please clarify it.??


I think this code can throw an InterruptedException.But it is given that this code throws an IllegalMonitorStateException.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will indeed throw IllegalMonitorStateException.

If you call wait() on an object, you have to be in a synchronized block that synchronizes on that object. In your example, there is a synchronized block, but it does not synchronize on obj - instead, it synchronizes on the current Thread object.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day,

I tried -



Getting the compiler error -




Regards,
Dan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This compiles better -

 
Author
Posts: 93
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get an IllegalMonitorStateException if your operations result in invalid thread state transitions. As Jesper de Jong correctly mentioned in the reply, since wait is called on obj without holding lock on obj, it results in this exception (note that in the syncrhonized block the lock is obtained on the current thread, and not on obj).

Consider this simple example:


This program crashes with IllegalMonitorStateException, like this:



The wait(int) method (with or without timeout value) should be called only after acquiring a lock: a wait() call adds the thread to the waiting queue of the acquired lock. If we don’t do that, there is no proper transition from the running state to timed_waiting (or waiting state in case a timeout value in not given) to happen. So, the program crashes by throwing an IllegalMonitorStateException exception.

The correct fix is to acquire the lock before calling wait(). In this case, we can declare the run() method synchronized:



Since the run() method is synchronized, wait() will add itself to the this object reference lock. Since there is no one calling the notify()/notifyAll() method, after a timeout of 1 second (1000 milliseconds) is over, it will return from the run() method. So, the wait(1000); statement behaves almost like a sleep(1000) statement; the difference is that calling wait() releases the lock on this object when it waits while sleep() call will not release the lock when it sleeps.

So the key thing to remember: Call wait and notify/notifyAll only after acquiring the relevant lock.
 
yojana kamabathula
Greenhorn
Posts: 18
Android Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou Ganesh Sg,Dan Drillich and Jesper de Jong
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code doesn't compile. Answer A means the code would throw InterruptedException while running. Your code has a compile error where obj.wait can throw InterruptedException and the exception isn't handled and the method is not declared as throwing it, so the class can't compile. So Answer A is not applicable here.

Of course the code from the question has the same problem. So the question seems flawed.

Once you fix the compile error, and move the waitForSignal call into the Thread's run method so that it gets called by the sd thread instead of the main thread, you should have something like:

public class ThreadStateProblem extends Thread {  
   public void run() {  
       waitForSignal();
   }  

   void waitForSignal() {
       Object obj = new Object();
       synchronized (Thread.currentThread()) {
           try {
               obj.wait();
           } catch (InterruptedException e) {
               Thread.currentThread().interrupt();
               e.printStackTrace();
           }
           obj.notify();
       }
   }

   public static void main(String []s) {  
       ThreadStateProblem sd =new ThreadStateProblem();
       sd.start();
   }    
}
then you should get the expected result:

Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
   at java.lang.Object.wait(Native Method)
   at java.lang.Object.wait(Object.java:503)
   at ThreadStateProblem.waitForSignal(ThreadStateProblem.java:10)
   at ThreadStateProblem.run(ThreadStateProblem.java:3)
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic