File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread exception doubt

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not clear...can you please explain more.
I think synchronized() can take a Thread object as argument.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic