The Java™ Tutorials — Concurrency :Interrupts 中断

来源:互联网 发布:五十而知天命的意思 编辑:程序博客网 时间:2024/06/09 16:04

The Java™ Tutorials — Concurrency :Interrupts 中断

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/interrupt.html

关键点

  • 响应中断的方法: 
    • 捕获InterruptedException,如果代码有抛出此异常的可能。
    • 如果代码无法抛出InterruptedException,那么利用Thread.isInterrputed轮询中断标记位是否为真。 
      • 在复杂方法中,利用此手段时,可以在Thread.isInterrupted为真时人为抛出一个InterruptedException,这样可以把代码的中断处理集中到catch块中
  • 中断机制的实现:利用内置的中断状态标记 
    • 调用Thread.interrupt会为中断标记置真,调用静态方法Thread.interrupted检查中断时,此标识就会被清除。
    • 非静态的isInterrupted方法用于一个线程查找另一个线程的中断状态,它是不会改变中断状态标识的
    • 根据惯例,任何因抛出InterruptedException异常而退出的方法都应该清除中断状态标识

全文翻译

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方法来发送中断信号。为了让中断机制可以正确工作,被中断的线程必须为它自己的中断提供支持。

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. 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:

一个Thread如何为自己的中断提供支持?这取决于它现在正在干什么。如果一个线程正在频繁调用可抛出InterruptedException异常的方法,它只需在捕获到这个异常时,简单地从run方法中返回就可以了。例如,假设SleepMessages样例中的中心消息循环队列正在运行一个线程的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.

根据惯例,任何因抛出InterruptedException异常而退出的方法都应该清除中断状态标识。然而,完全有可能因为另一个线程调用了interrupt方法而立即将中断状态标识置回。

0 0
原创粉丝点击