Java中的Thread.interrupt()和Thread.currentThread.interrupt()有什么区别? [英] What's the difference between Thread.interrupt() and Thread.currentThread.interrupt() in Java?

查看:142
本文介绍了Java中的Thread.interrupt()和Thread.currentThread.interrupt()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Thread.interrupt()和Thread.currentThread.interrupt()是做同样的事情还是给出相同的结果?如果没有,有什么区别?

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?

类似的问题是:Thread.sleep()和Thread.currentThread.sleep()之间的区别是什么,因为它们看起来是一样的感觉?

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

推荐答案

Thread.interrupt()方法中断特定实例引用的线程:

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

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

x变量可以引用任何线程实例。

The x variable can refer to any thread instance.

Thread.currentThread()。interrupt()方法与之前相同,但应用于当前Thread,只中断当前执行的线程。它相当于:

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();

关于 Thread.sleep() Thread.currentThread()。sleep()没有区别。 sleep()线程类,并且对您调用它的方式没有任何影响。调用它会导致当前执行的线程暂停指定的时间。

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.

这篇关于Java中的Thread.interrupt()和Thread.currentThread.interrupt()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆