What's the difference between Thread.interrupt() and Thread.currentThread.interrupt() in Java?

纵饮孤独 提交于 2020-01-23 03:50:07

问题


I wonder that whether Thread.interrupt() and Thread.currentThread.interrupt() do the same thing or will give the same result? If not, what's the difference?

The similar quesiton is: what's the difference between Thread.sleep() and Thread.currentThread.sleep() since they seems make the same sense?


回答1:


The Thread.interrupt() method interrupts the specific Thread that the instance references to:

Thread x = getSomeThreadInstance();
x.interrupt();

The x variable can refer to any thread instance.

The Thread.currentThread().interrupt() method is the same as before, but applied to the current Thread, interrupting only the current thread of execution. It is equivalent to:

Thread x = Thread.currentThread();
x.interrupt();

About Thread.sleep() and Thread.currentThread().sleep() there is no difference. sleep() is a static method on the Thread class, and makes no difference on the way you call it. Calling it causes the current thread of execution to pause for the indicated amount of time.

Nonetheless, one should not call static method on an instance, which means static method should be called in a static way.



来源:https://stackoverflow.com/questions/4801317/whats-the-difference-between-thread-interrupt-and-thread-currentthread-interr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!