Forums Register Login

Thread: wait() and notify()

+Pie Number of slices to send: Send
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.

}
+Pie Number of slices to send: Send
operator is still null because you never give it a value.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 !!!
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1504 times.
Similar Threads
Discussing errata for K&B, SCJP 6
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
Thread Communication using wait() and notify()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:28:40.