File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about notify being called at random by the JVM 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 "question about notify being called at random by the JVM" Watch "question about notify being called at random by the JVM" New topic
Author

question about notify being called at random by the JVM

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Hi,

In the K & B book chapter on Threads, page 755 says,

There's also a possible situation called spontaneous wakeup that may exist in some situations -- a thread may wake up even though no code has called notify() or notifyAll(). (At least, no code that you know about has called these methods. Sometimes the JVM may call notify for reasons of its own, or code in some other class calls it for reasons you just don't know.)


so, I understand that this means you have to check why a thread was awakened. But, why would the JVM just randomly call notify() on objects?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Read this:
http://en.wikipedia.org/wiki/Spurious_wakeup
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820


"Though there were indeed some members of the working group who argued that it was theoretically possible to imagine that there might be such an implementation, that wasn't really the reason. (And they were never able to prove it.) POSIX threads were the result of a lot of tension between pragmatic hard realtime programmers and largely academic researchers. Spurious wakeups are the mechanism of an academic computer scientist clique to make sure that everyone had to write clean code that checked and verified predicates!

"But the (perhaps) largely spurious (or at least arcanely philosophical) 'efficiency' argument went over better with the realtime people, and the real reason was usually relegated to second place in the rationale.

"I've thought many times about how you might construct a correct and practical implementation that would really have spurious wakeups. I've never managed to construct an example. Doesn't mean there isn't one, though, and it makes a good story."


you guys are messing with me, right?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Tim McGuire wrote:
you guys are messing with me, right?

No.
Look also here if you don't trust me (javaspec): http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.8
Thread t does not execute any further instructions until it has been removed from m's wait set. The thread may be removed from the wait set due to any one of the following actions, and will resume sometime afterward.

* A notify action being performed on m in which t is selected for removal from the wait set.
* A notifyAll action being performed on m.
* An interrupt action being performed on t.
* If this is a timed wait, an internal action removing t from m's wait set that occurs after at least millisecs milliseconds plus nanosecs nanoseconds elapse since the beginning of this wait action.
* An internal action by the implementation. Implementations are permitted, although not encouraged, to perform "spurious wake-ups" -- to remove threads from wait sets and thus enable resumption without explicit instructions to do so. Notice that this provision necessitates the Java coding practice of using wait only within loops that terminate only when some logical condition that the thread is waiting for holds.

All unix/linux systems implement POSIX thread library - JVM implementations on unix/linux usually make use of this library.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about notify being called at random by the JVM