10

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...

Community
  • 1
  • 1
Jazz
  • 3,691
  • 4
  • 17
  • 6
  • 4
    This question is so close to http://stackoverflow.com/questions/2077216/java-thread-currentthread-sleepx-vs-thread-sleepx that one shameless plagarist just cut-and-pasted the accepted answer as his answer to this question. – Stephen C Jul 24 '10 at 17:17
  • @Stephen C maybe it's someone else who's shameless for asking redundant questions? – Dave O. Jul 24 '10 at 23:55
  • @Dave - @Jazz has only asked 2 questions, and the other one wasn't redundant. Besides, everyone knows that plagarism is cheating. Children are taught this in primary school, and anyone who gets to university cannot failed to have learned that it is a big NoNo. – Stephen C Jul 25 '10 at 03:08
  • Every operation delays a Thread to some degree. However, if you specically want to introduce a delay you can; busy wait, use Object.wait(timeout), Queue.poll(timeout, TimeUnit), Selector.select(timeout), Socket.read() with a timeout or any other function which has a timeout. It all depends on why you want to delay your thread. – Peter Lawrey Jul 28 '10 at 07:00

2 Answers2

10

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.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • TimeUnit itself will run Thread.sleep() from Java API: Perform a Thread.sleep using this unit. – mhshams Jul 25 '10 at 05:12
  • @Mohammad shamsi, Yes, but using Object.wait() does not require using the Thread class. Strictly speaking though, TimeUnit is an alternative, even if the current implementation simply delegates to Thread.sleep(). – Tim Bender Jul 25 '10 at 07:29
-4

1 - they are same.

2 - Thread.sleep is the only way to delay in Java unless you implements your own way ;)

mhshams
  • 16,384
  • 17
  • 55
  • 65