简体   繁体   中英

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 between 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM