aspose file tools*
The moose likes Threads and Synchronization and the fly likes What's the difference between the methods sleep() and wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What Watch "What New topic
Author

What's the difference between the methods sleep() and wait()

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Question: What's the difference between the methods sleep() and wait()

Answer: The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

Is that right? A sleep thread can be interrupted by another thread too. I'm a little confused about: sleep/interrupt vs. wait/notify


To be or not to be. It's a question.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Another important difference is that wait gives up the monitor on the object (which is in fact why you need to call it on an object).

The uses are totally different. You will use sleep when you just want the current thread to sleep for some time. You use wait when you want the thread to wait for an event in another thread, that is signaled via notify/notifyAll.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: What's the difference between the methods sleep() and wait()