• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

wait() will wait forever

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic