sachin
sachin

Reputation: 1386

How Java Thread Class Determines Which thread?

While Going through the java tutorial on sun site, I see following piece of code:

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? eg. I expected it to be of type: this.interrupted() or Thread.interrupted(this).

Upvotes: 1

Views: 255

Answers (2)

Robert Munteanu
Robert Munteanu

Reputation: 68268

It looks at 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.

Upvotes: 4

Sander
Sander

Reputation: 26374

Thread.interrupted() will execute in the same thread as your loop, so that method will simply check whether the current thread has been interrupted.

Upvotes: 0

Related Questions