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

后端 未结 4 741
清歌不尽
清歌不尽 2020-12-04 13:33

I have this in my code

Thread.currentThread().sleep(x);

Eclipse tells me to use the static

Thread.sleep(x); 
4条回答
  •  日久生厌
    2020-12-04 13:52

    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.

提交回复
热议问题