aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() and notify() in K&B 5.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() and notify() in K&B 5.0" Watch "wait() and notify() in K&B 5.0" New topic
Author

wait() and notify() in K&B 5.0

Buhi Mume
Greenhorn

Joined: Jul 16, 2006
Posts: 17
from K&B 5.0 book, pg. 720


Immediately after b.start (line 6), is it guaranteed that ThreadA will continue its execution? Could it be that ThreadB will execute its run() method first?

If I understand correctly, there is no guarantee which one goes first. Help?


Buhi

(modofied to indicate K&B 5.0)
[ July 28, 2006: Message edited by: Barry Gaunt ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

If I understand correctly, there is no guarantee which one goes first. Help?


Yes. You understood correctly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Buhi Mume
Greenhorn

Joined: Jul 16, 2006
Posts: 17
O oh... does this mean the book explanation (on page 721) is wrong?

As soon as line 4 calls the start() method, ThreadA will continue
with the next line of code in its own class, ...


If it is wrong, how to correct it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Buhi Mume:
O oh... does this mean the book explanation (on page 721) is wrong?

If it is wrong, how to correct it?


I don't have enough information to determine the content of the statement, but thread A does continue with the next line of code in its own class. Now, whether it will do it before thread B starts executing the run() method -- that is not guarranteed.

Henry
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
Yes.

The book (p. 529 in 1.4 book) fails to point out this problem.

I guess the example is very basic, so as to not overwhelm the reader.

In the following book example, both threads are in an infinite loop. In this case, it doesn't matter which thread gets the lock first. If the notifying thread notifies the other thread before it starts waiting, it will notify it again the next time through the loop.


SCJP 1.4 - 95%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() and notify() in K&B 5.0