wait : Java Glossary

wait
Waiting is much like Thread sleeping. It consumes no cpu cycles. It is not the same thing as a tight test loop. A Thread suspends execution (does nothing), until some other thread gives it the go-ahead to resume execution with a notify().

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.

Learning More

Oracle’s Javadoc on Object.wait and Object.notify : available:
Oracle’s Javadoc on Thread class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/wait.html J:\mindprod\jgloss\wait.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 16,437.