Object.wait() and Object. notify() are methods used to coordinate Threads. Note these are methods of all objects, not just Threads. These methods are not something you will use directly in ordinary code. For most tasks, you would use one of the classes in the java.util.concurrent package.
Let’s pretend though you were writing some sort of producer-consumer logic from scratch where thread A produced objects for Thread B to consume.
In the ideal situation, with a multiple CPU (Central Processing Unit) machine, both Threads would whip along at full bore, but if B got behind consuming, (full queue), (Lucy in the chocolate factory), then A should wait() for B to catch up and B should notify() A when A can start working again. Similarly if A can’t keep up producing fast enough, (empty queue), B should wait() and A should notify() B when B can start working again.
You need an arbitrary rendezvous object for one Thread to wait() on and the other to notify(). It can get hairier with multiple waiters and multiple notifiers and timeouts on the waits.
This page is posted |
http://mindprod.com/jgloss/wait.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\wait.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[18.226.17.251] |
| |
Feedback |
You are visitor number | |