很多Java开发人员(包括我),尤其是刚进入软件行业的新手,认为Java设置线程中断就是表示线程停止了,不往前执行了,
其实不是这样的,线程中断只是一个状态而已,true表示已中断,false表示未中断
设置线程中断不影响线程的继续执行,但是线程设置中断后,线程内调用了wait、jion、sleep方法中的一种, 立马抛出一个 InterruptedException,且中断标志被清除,重新设置为false。
该如何让线程真正停止不往前执行呢:
真正让线程停止下来(阻塞),Java提供了一个较为底层的并发工具类:LockSupport,该类常用的方法有两个,1
park(Object blocker) 表示阻塞指定线程,参数blocker当前线程对象 2 unpark(Thread thread) 唤醒指定线程,参数thread指定线程对象
示例: