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

Omu picture Omu · Jan 16, 2010 · Viewed 76k times · Source

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?

Answer

Sean Owen picture Sean Owen · Jan 16, 2010

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.