• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread.sleep vs Object.wait

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do you decide to use Thread.sleep(long)
or Object.wait(long) ? Any gotchas that
I should watch out for ?
Thanks
Pho
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic