Documentation

The Java™ Tutorials
Hide TOC
Interrupts中断
Trail: Essential Java Classes
Lesson: Concurrency
Section: Thread Objects

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. 线程通过调用要中断的线程的线程对象上的interrupt来发送中断。For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.为了使中断机制正常工作,被中断的线程必须支持自己的中断。

Supporting 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. 如果线程经常调用抛出InterruptedException的方法,那么它只需在捕获该异常后从run方法返回。For example, suppose the central message loop in the SleepMessages example were in the run method of a thread's Runnable object. 例如,假设SleepMessages示例中的中心消息循环位于线程的Runnable对象的run方法中。Then it might be modified as follows to support interrupts:然后可以对其进行如下修改,以支持中断:

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? 如果线程运行很长时间而没有调用抛出InterruptedException的方法,该怎么办?Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. 然后它必须定期调用Thread.interrupted,如果接收到中断,它将返回trueFor example:例如:

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. 调用Thread.interrupt设置此标志。When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. 当线程通过调用静态方法thread.interrupted检查中断时,中断状态被清除。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.一个线程用来查询另一个线程的中断状态的非静态isInterrupted方法不会更改中断状态标志。

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. 按照惯例,任何通过抛出InterruptedException退出的方法都会在抛出时清除中断状态。However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.然而,总是有可能通过另一个调用interrupt的线程立即再次设置中断状态。


Previous page: Pausing Execution with Sleep
Next page: Joins