Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait() will wait forever

 
Mostafa Radwan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Consider the following form K&B:


My Question is wait() in line 9 will wait forever if code from 19 to 24 worked all at first . Is that ture ? and this can be solved my wait() version with predetermined period of time ?

Thanks,
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mostafa Radwan:
... My Question is wait() in line 9 will wait forever if code from 19 to 24 worked all at first. Is that ture? ...

In theory, yes. Although it's extremely unlikely that the thread scheduler would decide to call b's run method immediately after the call to start and before the next line of code can obtain b's lock.
Originally posted by Mostafa Radwan:
...and this can be solved my wait() version with predetermined period of time? ...

Yes. If this were to happen (which I've forced by putting a 1/10 second sleep call in the code below), the wait method would time out and the thread could run again without notification.

Compile and run the code below to demonstrate this.
 
Juan Handal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mostafa and Marc


Mostafa posted:
My Question is wait() in line 9 will wait forever if code from 19 to 24
worked all at first.Is that true?

Marc quoted:
Yes.Although it's extremely unliked..............
I run the program excelent answer " the b.sleep(100)"
I got it,my question is:
Marc quoted:
"the wait method would time out and the thread could run again without
notification"
Since Thread B is not the lock's owner,how come be notified?
Please I need to clarify this point
Thanks

:roll:
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mostafa Radwan:

My Question is wait() in line 9 will wait forever if code from 19 to 24 worked all at first . Is that ture ?

Mmmm. Maybe my brain's not firing on all cylinders here, but I don't think so.

b.wait() is called in main(), but b.run() calls notify(). So b is supposed to wait on itself and notify itself?

This is perverse code and I agree, it looks like an intentional race condition. I assume it's only meant to stress an academic point about the wait/notify model.
 
Adison Aei
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so,when the main call wait on b then it's will give up
the lock and so ThreadB it's self can accquire a lock(in the case main
get a lock first)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic