目录
vt.& vi. 打断(别人的话等); 阻止; 截断;
vt. 暂停; 中断; 打扰; 妨碍;
vi. 打断;
n. 中断; 暂停;
不管是及物动词还是不及物动词亦或是名称都是中断、暂停的意思
第一个问题:那么在Thread.interrupt()中,是不是意味着暂停当前线程呢?是的
第二个问题:如果是暂停当前线程,那么当前线程必须是在运行状态?不是,如果当前线程阻塞了,会清空中断标识,而且抛出异常
/**
* Interrupts this thread.
* 中断、暂停这个线程
* <p> Unless the current thread is interrupting itself, which is
* always permitted, the {@link #checkAccess() checkAccess} method
* of this thread is invoked, which may cause a {@link
* SecurityException} to be thrown.
* 除非当前线程正在中断它自己,线程自己中断自己是允许的,调用会抛出SecurityException异常的
checkAccess()方法
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
* 如果当前线程正在阻塞中,因为调用了Object对象的wait()方法,wait(long)方法,wait(long,
int),或者Thread类的join()方法,join(long)方法,join(long, int)方法,sleep(long)方
法,或者sleep(long, int) ,那么当前线程的中断状态会被清除,以及它当前线程会收到
InterruptedException 异常
* <p> If this thread is blocked in an I/O operation upon an {@link
* java.nio.channels.InterruptibleChannel InterruptibleChannel}
* then the channel will be closed, the thread's interrupt
* status will be set, and the thread will receive a {@link
* java.nio.channels.ClosedByInterruptException}.
在可中断的通道上面,如果当前线程在一个I/O操作中处于阻塞状态,然后将关闭通道,设置线程的中
断状态,而且线程会收到一个ClosedByInterruptException异常
*
* <p> If this thread is blocked in a {@link java.nio.channels.Selector}
* then the thread's interrupt status will be set and it will return
* immediately from the selection operation, possibly with a non-zero
* value, just as if the selector's {@link
* java.nio.channels.Selector#wakeup wakeup} method were invoked.
*
* <p> If none of the previous conditions hold then this thread's interrupt
* status will be set. </p>
* 如果出现没有前面的情况,设置这个线程的中断状态
* <p> Interrupting a thread that is not alive need not have any effect.
* 中断一个没有运行的线程不需要产生任何效果
* @throws SecurityException
* if the current thread cannot modify this thread
*
* @revised 6.0
* @spec JSR-51
*/
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();
}