Interrupts

Interrupts
中断
An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate. This is the usage emphasized in this lesson.
中断表明线程应该停止正在做的事并且去做其他的事。这个时候程序员决定线程怎样正确的回应中断,但是线程终止非常普遍。这是本节着重强调的用法。
A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.
线程通过调用Thread对象中的interrupt方法发送interrupt使线程中断。为了使中断机制正确工作,中断的线程必须支持它自己的中断。

Supporting Interruption
支持中断(Interruption)
How does a thread support its own interruption? This depends on what it's currently doing. If the thread is frequently invoking methods that throw
InterruptedException, it simply returns from the run method after it catches that exception. For example, suppose the central message loop in the SleepMessages example were in the run method of a thread's Runnable object. Then it might be modified as follows to support interrupts:
一个线程怎样支持自己中断呢?这个依赖它当前在做什么。如果线程在调用方法时频繁的抛出异常,它会在捕获异常后从这个run方法中返回。比如,假设在SleepMessages例子中主要的for循环是在一个线程的Runnable对象的run方法中。它可以改成下面这个以支持中断

for (int i = 0; i < importantInfo.length; i++) {
// Pause for 4 seconds
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// We've been interrupted: no more messages.
return;
}
// Print a message
System.out.println(importantInfo[i]);
}
Many methods that throw InterruptedException, such as sleep, are designed to cancel their current operation and return immediately when an interrupt is received.
许多方法会抛出InterruptedException,比如sleep,被设计取消它们当前的操作并且当接受到中断时立即返回

What if a thread goes a long time without invoking a method that throws InterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:
如果线程很长时间没有调用会抛出InterruptedException异常的方法怎么样呢?这时可以定期的调用Thread.interrupted,如果接收到了中断它会返回true。

for (int i = 0; i < inputs.length; i++) {
heavyCrunch(inputs[i]);
if (Thread.interrupted()) {
// We've been interrupted: no more crunching.
return;
}
}
In this simple example, the code simply tests for the interrupt and exits the thread if one has been received. In more complex applications, it might make more sense to throw an InterruptedException:
在这个简单的例子里,代码简单的测试了中断和线程接收到中断的退出。在更复杂的应用程序中,可能要更加注意抛出InterruptedException异常
if (Thread.interrupted()) {
throw new InterruptedException();
}
This allows interrupt handling code to be centralized in a catch clause.
这个允许中断处理代码集中在一个catch块里
The Interrupt Status Flag
中断状态标识
The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.
中断机制是使用一个称作中断状态的内部标识来实现的。调用Thread.interrupt设置这个标识。当线程通过调用静态方法Thread.interrupted检查中断时,中断状态会被清除。非静态的isInterrupted方法被用作一个线程查询另外一个线程的中断状态,这个不会改变中断状态标识。
By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.
按照惯例,任何方法通过抛出异常退出会清除中断状态。然而,极可能中断状态会因为其他的线程调用interrupt而立即被再次设置。

 

posted @ 2012-05-20 22:24  yuwenxing  阅读(403)  评论(0编辑  收藏  举报