Java: Difference in usage between Thread.interrupted() and Thread.isInterrupted()?

后端 未结 9 685
南旧
南旧 2020-12-12 16:28

Java question: As far as I know, there are two ways to check inside a thread whether the thread received an interrupt signal, Thread.interrupted() and Thr

9条回答
  •  囚心锁ツ
    2020-12-12 16:47

    interrupted() method is a static method of class thread checks the current thread and clear the interruption "flag".i.e. a second call to interrupted() will return false.

    isInterrupted() method is an instance method; it reports the status of the thread on which it is invoked. it does not clear the interruption flag.

    If the flag is set, it will remain set after calling this method.

    Thread myThread = ...;
    if (myThread.interrupted()) {} //error
    
    Thread.interrupted()//right
    
    if (myThread.isInterrupted()) {} // Right
    

提交回复
热议问题