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

wait() or thread.sleep()

Jackie Davis
Greenhorn

Joined: Mar 04, 2006
Posts: 23
Hi, having read some of the other posts I am not sure which method to use. I want to be able to pause running through some code for 1000ms and then restart. I have two methods, one running straight after the other. I would like to be able to delay starting the 2nd method for a short period.

Thanks
IM Mahesh
Greenhorn

Joined: Mar 09, 2006
Posts: 18
Difference in these APIs is that Thread.speep(long millis) causes to sleep the current thread for given period of time. It does not release the monitor which it has been aquired.. While Thread.wait(long millis) causes to wait for given period of time or will also exit from wait condn if any other thread calls Thread.notify() or Thread.notifyAll() on the same object on which this thread is synchronized. Also it releases the monitor that it has aquired.
Now in your case as you just want to pause the current thread for particular period of time should use Thread.speep(1000).


Mahesh
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by IM Mahesh:
Thread.speep(long millis) causes to sleep the current thread for given period of time. It does not release the monitor which it has been aquired.


Thread.sleep() does not acquire a lock. It has nothing to do with synchronisation.

If the code is holding any locks before sleep(), it still holds them during sleep(). It is often a bad idea to hold any locks during sleep.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
IM Mahesh
Greenhorn

Joined: Mar 09, 2006
Posts: 18
You are right Peter,
Sorry for the confusion happened. By mistake i was assuming that the sleep() method is going to be called in synchronized block, which is not always true.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() or thread.sleep()
 
Similar Threads
Session Termination
Client Application
unable to view index.html pahe in webapps folder
threads!
Breaking out of forever loop