• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Buhi Mume
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand correctly, there is no guarantee which one goes first. Help?


Yes. You understood correctly.

Henry
 
Buhi Mume
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic