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

thread

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
what is the difference between wait() and sleep()?

regards sandy
Casttro Francis
Greenhorn

Joined: Oct 08, 2008
Posts: 25
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


Any fool can write code that a computer can understand. Good programmers write code that humans can understand....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Not convinced. I think you have got the two the wrong way round. Start here.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
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.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
off to the intermediate forum


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread