What does java.lang.Thread.interrupt() do?

前端 未结 9 2395
日久生厌
日久生厌 2020-11-22 06:01

Could you explain what java.lang.Thread.interrupt() does when invoked?

9条回答
  •  Happy的楠姐
    2020-11-22 06:46

    Thread interruption is based on flag interrupt status. For every thread default value of interrupt status is set to false. Whenever interrupt() method is called on thread, interrupt status is set to true.

    1. If interrupt status = true (interrupt() already called on thread), that particular thread cannot go to sleep. If sleep is called on that thread interrupted exception is thrown. After throwing exception again flag is set to false.
    2. If thread is already sleeping and interrupt() is called, thread will come out of sleeping state and throw interrupted Exception.

提交回复
热议问题