aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait(), notify() in non synchronized method 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 "wait(), notify() in non synchronized method" Watch "wait(), notify() in non synchronized method" New topic
Author

wait(), notify() in non synchronized method

P Ventura
Ranch Hand

Joined: Jan 24, 2007
Posts: 42
Hi,
what happens if wait()/notify() is present in a non synchronized code?
An exception is thrown?


Objective: SCJP 1.5<br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html" target="_blank" rel="nofollow">API</a><br /> <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />Double n1 = Double.NaN; Double n2 = Double.NaN;<br /> n1.equals(n2) // true even though Double.NaN != Double.NaN<br />-0.0 == +0.0; // true<br />Double n1 = -0.0;Double n2 = +0.0;<br />n1.equals(n2) // false even though -0.0 == +0.0<br /></font></pre><hr></blockquote>
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Yes. The system will throw an IllegalMonitorStateException if it invokes wait/notify/notifyAll in a non-synchronized context. Try it out and see for yourself.

In addition, not just any synchronized context will do. This code will also throw an IllegalMonitorStateException:

[ November 05, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: wait(), notify() in non synchronized method