中斷(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()方法,中斷狀態老是有可能會當即被從新設置。 

相關文章
相關標籤/搜索