GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread exception doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread exception doubt" Watch "Thread exception doubt" New topic
Author

Thread exception doubt

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

1- IllegalStateException
2- Runs fine

Answer says (1);
But my question is, this code should throw IllegalMonitorException.

I dont find any. any relationship between IllegalStateException and IllegalMonitorException.

Please clarify!

Source of doubt: VooDoo


Thanks,
cmbhatt

(No need to shout in this forum)
[ April 25, 2007: Message edited by: Barry Gaunt ]

cmbhatt
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

IllegalStateException state is thrown when the thread's life cycle has ended and you try to restart the thread.

Where IllegalMonitorException is thrown when you call the wait method in a non-synchronized context! When calling the wait method, that thread must have obtained the object's lock!

notify and notifyAll have to be called within a synchronized context as well.

Best of luck ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
That is alright Vassili!

But the question was confusing:

"What exception the given code may" throw?
In that case what should I choose, IllegalMonitorStateException was not one
of those answers.



Thanks,
cmbhatt
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
The synchronized keyword expects an object which the thread can obtain its lock, not a thread!!!

So it's an illegal state ==> an IllegalStateException is thrown !!!

Best of luck ...
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Not clear...can you please explain more.
I think synchronized() can take a Thread object as argument.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


The given code speaks loudly that the exception, that will be thrown is
IllegalMonitorStateException. That is because, wait() method is called in
the synchronized context; you first take lock of that object and then call
wait method inside that block, and then release the lock.

Alright, Vasilli & Megha, you agree upto this?

I am only concerned about the question (picked from a reputed mock exam),
It has several options and the given correct answer says IllegalStateException; There is no corelation between IllegalMonitorStateException and IllegalStateException;

As I know the IllegalThreadStateException is thrown when you call start() method on the thread more than once.


IllegalStateException is thrown when you call a method on scanner reference
when it is closed.
java.util.Scanner scan = new java.util.Scanner(System.in);
scan.nextInt();
scan.close();
scan.nextInt(); //IllegalStateException


But I am afraid of the bizarre answer of the original question posted by me on the top, regarding IllegalStateException.





Megha says,
I think synchronized() can take a Thread object as argument.


Definitely synchronized() can take Thread object; It means no more than
one thread can access that object at a time; I think this scenario in the
following way:





Thanks,
cmbhatt
[ April 25, 2007: Message edited by: Chandra Bhatt ]
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
There have been this mistake in my first reply when i said IllegalStateException is thrown when you restart a thread.

Sorry, i always have conflicts in this! It's an IllegalThreadStateException the one thrown when you restart the thread!

This example is throwing an IllegalStateException because it expects an object which its lock can be obtained by a thread and enter its synchronized code, not a thread!

Best of luck ...
vikram choudhary .
Greenhorn

Joined: Apr 25, 2007
Posts: 7
hi,


i think, this code will throw java.lang.IllegalMonitorStateException: not IllegalStateException ...
see, there are two things thread of execution and thread object having 1-1 mapping.
A thread object is just like other java objects so it also has a lock and any thread can acquire it.but remember, a thread of execution never hold the lock of its thread object but it can acquire by calling synchronized block or method.
that is the reason sometimes, run() is made synchronized.

so in the above code,
the current thread (means,thread of execution) will try to acquire the lock of
current thread (here, it is thread object) which is available. so no,
IllegalStateException.
but in the synchronized block you are calling wait on obj object whose lock is not acquired by thread(thread of execution) and throws IllegalMonitorStateException.

Am i right?
regards,
Vikram
preparing for scjp1.5
 
GeeCON Prague 2014
 
subject: Thread exception doubt