wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads (wait & notify) 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 "Threads (wait & notify)" Watch "Threads (wait & notify)" New topic
Author

Threads (wait & notify)

Rudolf Meerkotter
Greenhorn

Joined: Apr 03, 2008
Posts: 14
Hi there,

I came up with this bit of code below to practice wait() and notify(). Class Changeable has a boolean variable, notifies the Observer class from it's run method, which then stops waiting and prints Changeable's state. Observer checks for notifications from a while loop. All normal so far. When I run it, it seems that not all the notifications are received. Sometimes more, sometimes less, but never all.

Output varies, but looks like this:

Changeable running. State is: true 1
Changeable running. State is: true 2
Observer running. Changeable's state is true 1
Changeable running. State is: true 3
Observer running. Changeable's state is true 2
Changeable running. State is: true 4
Observer running. Changeable's state is true 3
Changeable running. State is: true 5
Changeable running. State is: true 6
Observer running. Changeable's state is true 4
Changeable running. State is: true 7
Observer running. Changeable's state is true 5
Changeable running. State is: true 8
Observer running. Changeable's state is true 6
Changeable running. State is: true 9
Observer running. Changeable's state is true 7
Changeable running. State is: true 10
Observer running. Changeable's state is true 8

Here's the code:


SCJP 5, SCWCD 5, SCBCD 5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

If a notification is sent, and there isn't a thread waiting for it, it will be lost.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads (wait & notify)