aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread.sleep vs Object.wait Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread.sleep vs Object.wait" Watch "Thread.sleep vs Object.wait" New topic
Author

Thread.sleep vs Object.wait

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

When do you decide to use Thread.sleep(long)
or Object.wait(long) ? Any gotchas that
I should watch out for ?
Thanks
Pho


Regards,

Pho
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi,
Thread.sleep()is used to make the thread sleep for a specified time. This gives other threads CPU time.
wait()method is used to relinquish a object lock and then the thread enters the wait state.When a thread gets invokes a synchronized method it obtains that object's lock. If it now wants to give other threads the objects lock it will invoke the wait method. This will take the current thread to wait queue i.e. the thread stop executing. Now other threads which are competing for the object's lock will get an opportunity. The previous thread which is now in wait state will get an opportunity to continue executing from the point where it left off(i.e.where it called wait) only when any other threads call notify/notifyall() method. Of course the previous thread has to compete with other threads to get hold of lock. The wait,notify and notifyall methods can only be used within an synchronized block or method.
In short u use sleep to give other threads an opportunity to get CPU timw whereas wait is used when other threads need to get hold of an objects lock.

Originally posted by Pho Tek:
When do you decide to use Thread.sleep(long)
or Object.wait(long) ? Any gotchas that
I should watch out for ?
Thanks
Pho
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
adding to that reply
when a thread sleep it sleeps without reliquishing the lock so other waithing threads will not get the lock.
Whereas Thread.sleep is a way of judiciosly relinquishing the CPU occupation,Object.wait is an effective way of interthread communication.


Help me!Help you!!!
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Thanks guys!
But if I do this in my code:
synchronized(this){
this.wait(20000);
}
to simulate a sleep; will there be
any unforceeable problems. The code
segment being executed by this thread
will never be executed by other threads.
Thanks
Pho
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
using wait when u only want the excuting thread to sleep will cause the calling thread to sleep for that specified time no doubt,but even after the sleep time lapses it will not be able to excute the code after the wait method in the sync method (if there in any)until some other thread calls notify().
william
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.sleep vs Object.wait