中断(Interrupts)

中断(Interrupts)html

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.  中断是给线程的一个指示,告诉它应该中止正在作的事并去作其余事。一个线程究竟要怎么响应中断请求取决于程序员,不过让其终止是很广泛的作法。这是本文重点强调的用法。java

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()方法,发送中断信号。为了让中断机制正常工做,被中断的线程必须支持它本身的中断(即要本身处理中断)程序员

Supporting Interruption

中断支持api

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:  线程如何支持自身的中断?这取决于它当前正在作什么。若是线程正在频繁调用会抛InterruptedException异常的方法,在捕获异常以后,它仅需从run()方法中返回。例如,假设在SleepMessages的例子中,关键的消息循环在线程的Runnable对象的run方法中,代码可能会被修改为下面这样以支持中断:oracle

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()),被设计成接收到中断后取消它们当前的操做,并在当即返回。app

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。例如:less

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异常可能更有意义。 ide

if (Thread.interrupted()){
   throw new InterruptedException();
}

This allows interrupt handling code to be centralized in a catch clause.  这使得中断处理代码能集中在catch语句中。oop

The Interrupt Status Flag

中断状态旗号 
this

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.  按照惯例,任何经过抛出一个InterruptedException异常退出的方法,当抛该异常时会清除中断状态。不过,经过其余的线程调用interrupt()方法,中断状态老是有可能会当即被从新设置。 

相关文章
相关标签/搜索