开发者

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

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 be开发者_高级运维tween Thread.sleep() and Thread.currentThread.sleep() since they seems make the same sense?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜