线程之interrupt

interrupt概念

  interrupt翻译成中文为“打断”的意思,但实际上,interrupt()方法并非将一个线程打中断的意思。查看Thread.interrupt()方法的源码:

public void interrupt() {
        if (this != Thread.currentThread())
            checkAccess();

        synchronized (blockerLock) {
            Interruptible b = blocker;
            if (b != null) {
                interrupt0();           // Just to set the interrupt flag
                b.interrupt(this);
                return;
            }
        }
        interrupt0();
    }

  从以上源码可以知道,interrupt()方法只是设置了Thread对象中的一个标志位而已(Just to set the interrupt flag)。它的意义在于,线程可以通过这个标志位来决定需要做什么操作。

interrupt相关方法

  • interrupt()
    打断某个线程(设置标志位)
  • isInterrupted()
    查询某个线程是否被打断过(查询标志位)
  • interrupted()
    查询当前线程是否被打断过,并重置标志位
public class TestThread {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                System.out.println("Thread is interrupted");
                System.out.println(Thread.currentThread().isInterrupted());
            }
        });
        thread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}

输出如下:

Thread is interrupted
false

  可知当线程sleep时,如果调用了interrupt()方法,将会抛出InterruptedException异常。捕获异常之后,会重置interrupt标志位。

wait、sleep、join与interrupt

  当一个线程在wait、sleep或join时,如果调用了其interrupt()方法,则会抛出InterruptedException异常。

synchronized与interrupt

  当一个线程在等待锁的时候,其能否被interrupt打断?

public class TestThread {
    public static void main(String[] args) {
        final Object o = new Object();
        new Thread(() -> {
            synchronized (o) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Thread thread = new Thread(() -> {
            System.out.println("thread 获得锁");
        });
        thread.start();
        thread.interrupt();
    }
}

输出:

thread 获得锁

  可知当线程等待锁时,并不会被打断。

ReentrantLock与interrupt

public class TestThread {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            lock.lock();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.unlock();
        });

        Thread t2 = new Thread(() -> {
            lock.lock();
            System.out.println("t2 获得锁");
            lock.unlock();
        });
        t1.start();
        t2.start();
        t2.interrupt();
    }
}

输出:

t2 获得锁

  此时,线程在lock等待时依然不会被打断。如果希望线程在lock等待时可以被打断,可使用lockInterruptibly方法。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值