下面我们来看下LockSupport对应中断的响应性
public static void t2() throws Exception
{
Thread t = new Thread(new Runnable()
{
private int count = 0;
@Override
public void run()
{
long start = System.currentTimeMillis();
long end = 0;
while ((end - start) <= 1000)
{
count++;
end = System.currentTimeMillis();
}
System.out.println("after 1 second.count=" + count);
//等待或许许可
LockSupport.park();
System.out.println("thread over." + Thread.currentThread().isInterrupted());
}
});
t.start();
Thread.sleep(2000);
// 中断线程
t.interrupt();
System.out.println("main over");
}

本文通过一个示例演示了如何使用LockSupport.park使线程进入等待状态,并且当线程被中断时,线程的中断状态会被设置为true,但不会抛出InterruptedException。
979

被折叠的 条评论
为什么被折叠?



