aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() will wait forever Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() will wait forever" Watch "wait() will wait forever" New topic
Author

wait() will wait forever

Mostafa Radwan
Greenhorn

Joined: Sep 09, 2003
Posts: 25
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,


- Mostafa Radwan -
M.Sc | SCJP 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Juan Handal
Greenhorn

Joined: Jan 25, 2005
Posts: 29
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

Joined: Oct 25, 2000
Posts: 7292

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.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Adison Aei
Greenhorn

Joined: Oct 02, 2005
Posts: 1
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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() will wait forever
 
Similar Threads
Thread wait() and notify()
See this thread interaction code
Wait and Notify from KS&BB
wait() and join()
notify() question from Nikos