• Post Reply Bookmark Topic Watch Topic
  • New Topic

Example of wait and notify  RSS feed

 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi pals,

Would some one help me out !

when to use wait and notify ?

An example of wait and notify, where 2 threads access the same method synchronising themselves with wait and notify to access that method.
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Sun Java Tutorial has an example of using wait() and notifyAll() to control access to a shared field at http://java.sun.com/docs/books/tutorial/essential/concurrency/guardmeth.html. I can't think of a realistic example of controlling accessing to a single method using wait() and notifyAll().
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I should note that catching and ignoring InterruptedException like those examples is very bad form.
 
Jules Bach
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say you are modeling a fairground...

In my fairground there are 5 visitors, and only 3 dodgem cars. Visitors must wait until a car becomes free before using it, and a visitor will notify waiting vsistors once they have finished their ride for a random amount of time that a car may be available...

My thread class to represent visitors:




 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carey Evans:
The Sun Java Tutorial has an example of using wait() and notifyAll() to control access to a shared field at http://java.sun.com/docs/books/tutorial/essential/concurrency/guardmeth.html. I can't think of a realistic example of controlling accessing to a single method using wait() and notifyAll().



That was really help full !
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jules Bach:
Say you are modeling a fairground...

In my fairground there are 5 visitors, and only 3 dodgem cars. Visitors must wait until a car becomes free before using it, and a visitor will notify waiting vsistors once they have finished their ride for a random amount of time that a car may be available...

My thread class to represent visitors:






That should really be good !

But if its a bit easier, i could refer this link to my friends,

who are not that excellent at java.

Please get some other example to explain this concept.

some thing easier.!!!

Am not very clear about objecDoneLock' nextInt(1000)(random numbers.)

and synchronized block .


But if possible make this simpler attaining the task and explainig clearly about wait() and notify(). scenario is not a problem !

But thanks for the efforts you have given !
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!