Search code examples
javamultithreadinginterruptstatic-methods

How does Thread.interrupted() work in java?


Thread.interrupted() is static method. Then how it is able to modify the interrupted flag of instance objects?


This question is not duplicate of

because they don't address the question mentioned in the description of this post.


Solution

  • Modulo some details, it looks like you could write your own Thread.interrupted() as essentially:

    static boolean interrupted ( ) {
        Thread me = Thread.currentThread();
        return me.isInterrupted();
    }
    

    So the real question is how does the static function Thread.currentThread() find the current thread? This appears to be a bit of an unsolved mystery: How does Thread.currentThread() work? :) Serious answer: it's specific to the JVM and how threads are implemented.