是否需要同步对中断方法的调用?
查阅JavaDocs和方法的源代码,我发现这个:Thread.interrupt()
Java SE 7
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(); //1, Outside of the synchronized block
}
//...
private native void interrupt0();
可以看出,at 的本机方法调用位于同步块之外。那么,如果不将对方法的调用放入块中,是否安全?//1
interrupt()
synchronized
Thread t;
//something else
t.interrupt(); //Not in a synchronized block
它会是线程安全的吗?如果超过1个线程将尝试同时中断它怎么办?那么,本机方法将如何表现呢?interrupt0