aspose file tools*
The moose likes Threads and Synchronization and the fly likes thread that sleeps and thread that waits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread that sleeps and thread that waits " Watch "thread that sleeps and thread that waits " New topic
Author

thread that sleeps and thread that waits

Moni Patil
Greenhorn

Joined: Feb 03, 2005
Posts: 9
What is the differences between
a thread that sleeps by calling sleep
and
a thread that waits by calling wait.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42641
    
  65
A sleeping thread will wake after the amount of time specified in the sleep call, and will then resume execution.
A thread that called wait will not execute until a corresponding notify call is made by some other thread. That could happen almost immediately after calling wait, or it could happen much later, or maybe never.
So the difference is sleeping for a fixed amount of time vs. sleeping until an external event occurs.


Ping & DNS - my free Android networking tools app
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Java has an API that you can read on Suns website or download yourself. I like to download the API myself because its very useful. If you read it you will have the answers you seek.

sleep does not release any held monitors but wait does.
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

sleep does not release any held monitors but wait does.

If you can elaborate more?
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Thread.Sleep(long)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

Object.wait(long)
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.

wait() must be called from within a synchronize block, and when its called it releases the synchronization lock bka monitor. sleep() has nothing to do with synchronization and does not affect any monitors.
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

Lamont,
Gotcha. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread that sleeps and thread that waits