wood burning stoves 2.0*
The moose likes Java in General and the fly likes Thread: wait() and notify() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread: wait() and notify()" Watch "Thread: wait() and notify()" New topic
Author

Thread: wait() and notify()

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
I am studying Thread but I don't fully understand yet. I got NullPointerException from the program below.
Please shed a light.




// Below is the program I ran.

}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

operator is still null because you never give it a value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Rob Prime wrote:operator is still null because you never give it a value.


It probably is a dumb question.
I add a line: Operator operator = new Operator() but it waits forever, never come back.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Well, that's because of the "while (true)". That means never stop. However, even if that loop would end the thread would not. That's because there will never be a notify(All) on that very same Operator instance.

Try the following:
This will print "Waiting", then waits 5 seconds, then will alternatively print out "No more" and "Waiting" until you press CTRL+Z to terminate the JVM.
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
Rob Prime wrote:Well, that's because of the "while (true)". That means never stop. However, even if that loop would end the thread would not. That's because there will never be a notify(All) on that very same Operator instance.

Try the following:
This will print "Waiting", then waits 5 seconds, then will alternatively print out "No more" and "Waiting" until you press CTRL+Z to terminate the JVM.


Thank you very much !!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread: wait() and notify()
 
Similar Threads
Thread Communication using wait() and notify()
Probably found a bug in a code sample in Sierra & Bates SCJP 1.6 book (chapter 9 "Threads", p. 750)
Problem with Java 2 SCJP Certification Study Guide???
Threads and locking
Discussing errata for K&B, SCJP 6