| Author |
Please explain how does this code reaches the Exception code and prints 20 GOTO 30
|
Jacob Sonia
Ranch Hand
Joined: Jun 28, 2009
Posts: 164
|
|
Doubt in this for how the exception is thrown, how does it print 20 GOTO 30
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6603
|
|
|
Instead of catching Exception try catching the specific Exception type that is thrown by this code. What must you do before calling wait() ? Go through the thread topics a little more and you will understand the reason behind the exception.
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
sebastian tortschanoff
Ranch Hand
Joined: Mar 19, 2009
Posts: 68
|
|
The wait() method must be called from synchronized context otherwise a IllegalMonitorStateException might be thrown, when wait() is invoked on an object.
The code in main() is lega,l since Thread-Class implements Runnable-Interface.
The only way to acquire the Monitor of an Objext is to synchronize on it. Otherwise an Exception will be thrown, when wait(), notify() or notifyAll() is invoked on an object from non-synchronized context.
A thread becomes the owner of the object's monitor in one of three ways:
* By executing a synchronized instance method of that object.
* By executing the body of a synchronized statement that synchronizes on the object.
* For objects of type Class, by executing a synchronized static method of that class.
None of this happens in this example so an exceptions will be thrown.
|
Power from within.
Failed SCJP 2 times :-(
|
 |
Silke Bader
Greenhorn
Joined: Jul 04, 2009
Posts: 3
|
|
Method wait() is only valid in a synchronized method.
Since there is no synchronization an error is thrown and catched. Hence "20 GOTO 30" is printed.
|
 |
 |
|
|
subject: Please explain how does this code reaches the Exception code and prints 20 GOTO 30
|
|
|