This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain how does this code reaches the Exception code and prints 20 GOTO 30

 
Jacob Sonia
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Doubt in this for how the exception is thrown, how does it print 20 GOTO 30
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sebastian tortschanoff
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Silke Bader
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic