Thread.interrupted方法对LockSupport.park()的影响
先看,方法三
private static void test3() throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("park...");
LockSupport.park();//暂停t1线程
log.debug("unpark...");
log.debug("打断状态:{}", Thread.currentThread().isInterrupted());
}, "t1");
t1.start();
sleep(1);//主线程睡一秒
t1.interrupt();//主线程一秒后打断了线程t1,或者说唤醒t1线程
}
运行结果
23:51:56.423 c.Test14 [t1] - park...
23:51:57.423 c.Test14 [t1] - unpark...
23:51:57.423 c.Test14 [t1] - 打断状态:true
再看方法五
private static void test5() throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("park...");
LockSupport.park();//暂停t1线程
log.debug("unpark...");
log.debug("打断状态:{}", Thread.currentThread().isInterrupted());
LockSupport.park();//再次暂停t1线程
log.debug("unpark...");
}, "t1");
t1.start();
sleep(1);//主线程睡一秒
t1.interrupt();//主线程一秒后打断了线程t1
}
运行结果
23:57:48.661 c.Test14 [t1] - park...
23:57:49.660 c.Test14 [t1] - unpark...
23:57:49.660 c.Test14 [t1] - 打断状态:true
23:57:49.663 c.Test14 [t1] - unpark...
这里可以看到,LockSupport.park()
想再次暂停t1线程,但是失败了(打印语句都在49秒时执行)。
也就是说,打断标记为true
时,再次打断失效。
那么如何让LockSupport.park()
方法再次生效呢,这里就要用到Thread.interrupted()
。
方法六
private static void test6() throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("park...");
LockSupport.park();
log.debug("unpark...");
log.debug("打断状态:{}", Thread.interrupted());
LockSupport.park();
log.debug("unpark...");
}, "t1");
t1.start();
sleep(1);
t1.interrupt();
}
运行结果
00:08:03.385 c.Test14 [t1] - park...
00:08:04.384 c.Test14 [t1] - unpark...
00:08:04.384 c.Test14 [t1] - 打断状态:true
这里的Thread.interrupted()
也会返回打断标记true
,表示当前线程被打断过,但是其内部,除了返回打断标记,还会把打断标记设置为false
,表示清除打断标记。
这样子,LockSupport.park()
还可以再次生效。没有再继续向下运行。
总结
打断标记为真时,会让park
失效,调用Thread.interrupted()
会重置打断标记。