简体   繁体   English

Java 线程类如何确定哪个线程?

[英]How Java Thread Class Determines Which thread?

While Going through the java tutorial on sun site, I see following piece of code:在浏览 sun 站点上的 java 教程时,我看到以下代码:

for (int i = 0; i < inputs.length; i++) {
    heavyCrunch(inputs[i]);
    if (Thread.interrupted()) {
        //We've been interrupted: no more crunching.
        return;
    }
}

Since Thread.interrupted() is a static function, how does java knows which thread is calling it?既然 Thread.interrupted() 是一个静态函数,那么 java 怎么知道哪个线程在调用它呢? eg.例如。 I expected it to be of type: this.interrupted() or Thread.interrupted(this).我希望它的类型是:this.interrupted() 或 Thread.interrupted(this)。

It looks at Thread.currentThread() .它查看Thread.currentThread()


If you want to know how that works, it's a native method, and probably JVM-specific so there's no quick answer.如果你想知道如何工作,这是一个本地方法,而且很可能JVM特定的,所以没有快速的解答。

Thread.interrupted() 将在与您的循环相同的线程中执行,因此该方法将简单地检查当前线程是否已被中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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