Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.currentThread().sleep(time) v/s Thread.sleep(time); [duplicate]

Possible Duplicate:
Java: Thread.currentThread().sleep(x) vs. Thread.sleep(x)

whats the difference between...

Thread.currentThread().sleep(time)

and

Thread.sleep(time);

one more thing is there anyother method by which i can delay in program without using Thread Class...

like image 854
Jazz Avatar asked Jul 24 '10 16:07

Jazz


1 Answers

Thread.sleep() is a static method. There is no difference between calling Thread.sleep() and calling Thread.currentThread().sleep(). However, if you use eclipse, the latter form should give you a warning message as you are accessing the static method sleep() in a non-static way.

TimeUnit.sleep() is a fantastic alternative to Thread.sleep(). I personally prefer it because in most cases I want to sleep for whole seconds and I can easily and clearly do this with TimeUnit.SECONDS.sleep(5) as opposed to Thread.sleep(5*1000) or Thread.sleep(5000)

Edit: There is yet another alternative, but it is not at all a desirable alternative. Calling Object.wait() will halt execution in a similar fashion to Thread.sleep(). However, it is inadvisable to do so because first you have to use synchronize(Object) and this method is intended for wait/notify Thread synchronization. Also, there is no guarantee that the Thread will wait for the full time specified as spurious wake-ups from Object.wait() are acceptable for JVM implementations.

like image 161
Tim Bender Avatar answered Nov 15 '22 19:11

Tim Bender