• 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

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference between wait() and sleep()?

regards sandy
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait() : This function takes parameters as milliseconds and puts the thread in wait state for the desired time of the programmer after time passes the execution starts again.

Sleep() : This function is also used for same purpose using his function by java you can put a thread in sleep state .sleep does not contains any parameters so the thread will not be automatically start execution It needs a wake up signal again which can be Notify() or Notifyall().

So the main difference in Wait() and sleep() is wait takes time parameter and wait for specific time only and sleep throws a thread in sleep mode for unspecified time.

Better read Java API for more information
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not convinced. I think you have got the two the wrong way round. Start here.
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread will certainly be in non-active mode for a certain period of seconds as mentioned in parameters.
A wait(1000) method is similar to sleep() only difference being it can be interrupted meantime by notify or notifyAll() methods.
So
sleep(1000)-->cannot be interrupted
wait(1000) --> can be interrupted meantime
Regards.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
off to the intermediate forum
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ramya narayanan:
sleep(1000)-->cannot be interrupted


Are you sure? Then why can it throw an InterruptedException?

wait, unlike sleep, triest to acquire a lock on the object. wait() will wait indefinitely until the lock can be acquired (or until the call is interrupted), wait(long) and wait(long, long) will do the same but after the specified time has been reached it will no longer block. All three must be called in a block or method that synchronizes on the object on which wait is called.

sleep just puts the thread to sleep without any locking.
 
reply
    Bookmark Topic Watch Topic
  • New Topic