1.JAVA多线程(五)Java多线程之interrupted线程中断
1.1 interrupted(线程中断)
一个线程执行完毕之后会自动结束,如果在运行过程中发生异常也会提前结束。
通过调用一个线程的 interrupt() 来中断该线程,如果该线程处于阻塞、限期等待或者无限期等待状态,那么就会抛出 InterruptedException,从而提前结束该线程。但是不能中断 I/O 阻塞和 synchronized 锁阻塞。
对于以下代码,在 main() 中启动一个线程之后再中断它,由于线程中调用了 Thread.sleep() 方法,因此会抛出一个 InterruptedException,从而提前结束线程,不执行之后的语句。
package com.yuanxw.chapter5;
/**
* 中断线程
*/
public class InterruptThread {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(