Home  >  Q&A  >  body text

java的中断线程interrupt()方法,为什么还要把run()方法走完呢?那中断线程不就没有意义了吗?

当线程调用interrupt方法后,为什么还要运行完run方法呢?那中断线程还有什么意义?

高洛峰高洛峰2729 days ago727

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:35:34

    Java interrupt mechanism is a cooperative mechanism, which means that another thread cannot be directly terminated through interruption, and the interrupted thread needs to handle the interruption by itself. This is like parents at home telling their children to take care of their health, but whether and how their children take care of their health is entirely up to them.

    Detailed analysis of Java interrupt mechanism

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:35:34

    Java threads also have a way to prevent the run method from completing, and that is the stop method. However, this method has been deprecated by jdk. Why? If your thread is modifying data somewhere, be it a database or writing a file, and the stop method kills the thread without any explanation, will the data be chaotic or inconsistent? Therefore, Java threads use an interrupt mechanism, and you can capture this interrupt and perform the operations required after the interruption.

    reply
    0
  • Cancelreply