• Post Reply Bookmark Topic Watch Topic
  • New Topic

multithreading the wait method  RSS feed

 
Varun Goenka
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explain to me all the basic's of the wait method and how to use it to suspend and/or block a thread.also tell me how does one stop a thread?
Please help me!
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Varun,

A good place to start with this is the Java API reading about Object's wait and notify methods. Basically, the wait method will tell a thread to stop and wait until another thread calls notify. Notice that the wait method is on Object, so a thread waits on an object (it must hold the lock on the object to call wait). A typical example is a blocking queue. If a consumer calls get and there are no methods available, it will block (by waiting) until data becomes available. When a producer puts data in, the queue's notify method gets called and any waiting consumers get notified that data is available. These are very tricky methods to use correctly, so be careful and use them cautiously.

Jeff

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!