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

80,132

Solution 1

There is only one method, not two, and it is static. While you can call a static method via an instance reference, it's not good style. It indicates the programmer thinks he or she is calling an instance method. A confused programmer might be thinking he or she can cause another thread (not the current one) to sleep this way, when that's not what it does.

Both your lines of code do the same thing but the second is better style.

Solution 2

In Java, sleep is a static method. Both your examples do exactly the same thing, but the former version is confusing because it looks like it is calling a method on a specific object but it's not doing that at all. In your example it won't matter much, but it is more dangerous if you have the following:

someOtherThread.sleep(x);

This time it looks like you are telling some other thread to sleep, but in fact you are putting the current thread to sleep. The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object.

Solution 3

The two method calls are identical in behavior because they invoke the same method but using the class name (Thread in this case) rather than instance for accessing static fields and methods makes this static-ness clear. That is why this warning is produced.

But considering that static fields and methods are shown in a particular way in most IDEs (for example in italic font in Eclipse and IntelliJ IDEA), is this warning still necessary? Maybe not as much necessary as it was in the early days of Java that simple editors were in use.

Share:
80,132
Omu
Author by

Omu

https://youtu.be/h-K2sMmUlxA http://youtu.be/0fFLZuQ20Qw https://github.com/omuleanu/ValueInjecter http://demo.aspnetawesome.com http://prodinner.aspnetawesome.com

Updated on December 04, 2020

Comments

  • Omu
    Omu over 3 years

    I have this in my code

    Thread.currentThread().sleep(x);
    

    Eclipse tells me to use the static

    Thread.sleep(x); 
    

    instead, why? What's the difference, is there some difference in functionality at all between these 2 methods?

  • Chii
    Chii over 14 years
    +1 to mentioning that the programmer may want to make a particular thread sleep via someThread.sleep(), which it doesnt.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar about 11 years
    Do you mean both currentThread and someOtherThread will go for sleep while executing this single line "someOtherThread.sleep(x);" ??
  • Torque
    Torque about 11 years
    No. The current thread will go to sleep, no matter what Thread object .sleep is called on. You can't put other ThreadS to sleep (like that).
  • Dev Anand Sadasivam
    Dev Anand Sadasivam over 8 years
    To talk about thread,- it spawns asynchronously, although we may have synchronous things in threading. By nature things, all-the-things are asynchronous, although we find things which are synchronous sometime. None of of the things are synchronous even we intrude Quantum or Astronomy.