【Java併發編程學習】二、線程中斷

public class SleepInterrupt extends Object implements Runnable {
    @Override
    public void run() {
        System.out.println("in run() - enter normally");
        try {
            System.out.println("in run() - about to sleep for 20 seconds");
            Thread.sleep(20000);
            System.out.println("in run() - woke up");
        } catch (InterruptedException e) {
            System.out.println("in run() - interrupted while sleeping");
            /**
             * 處理完中斷異常後,返回run()方法入口,
             * 若是沒有return,線程不會實際被中斷,它會繼續打印下面的信息
             */
            return;
        }
        System.out.println("in run() - leaving normally");
    }
}
public class InterruptDemo {
    public static void main(String[] args) {
        SleepInterrupt sleepInterrupt = new SleepInterrupt();
        Thread thread = new Thread(sleepInterrupt);
        thread.start();
        // 主線程休眠2秒,從而確保剛纔啓動的線程有機會執行一段時間
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("in main() - interrupting other thread");
        /**
         * 當一個線程運行時,另外一個線程能夠調用對應的Thread對象的interrupt()方法來中斷它,
         * 該方法只是在目標線程中設置一個標誌,表示它已經被中斷,並當即返回
         */
        thread.interrupt();
        System.out.println("in main() - leaving");
    }
}
public class PendingInterrupt extends Object {
    public static void main(String[] args) {
        /**
         *  若是輸入了參數,則在main線程中中斷當前線程(亦即main線程),除了將中斷標誌設置爲true外,線程被中斷了,可是main線程
         *  仍然運行,main線程繼續監視實時時鐘,並進入try塊,一旦調用sleep()方法,它就會注意到待決中斷的存在,並拋出
         *  InterruptException.
         */
        if (args.length > 0) {
            Thread.currentThread().interrupt();
        }
        // 獲取當前時間
        long startTime = System.currentTimeMillis();
        /**
         *  若是線程在調用sleep()方法前被中斷,那麼該中斷稱爲待決中斷,它會在剛調用sleep()方法時,
         *  當即拋出InterruptedException異常
         */
        try {
            Thread.sleep(2000);
            System.out.println("[PendingInterrupt] was NOT interrupted");
        } catch (InterruptedException e) {
            System.out.println("[PendingInterrupt] was interrupted");
        }
        // 計算中間代碼執行的時間
        System.out.println("[PendingInterrupt] elapsedTime=" + (System.currentTimeMillis() -  startTime));
    }
}
/**
 * 使用Thread對象上調用isInterrupted()方法來檢查任何線程的中斷狀態
 * 線程一旦被中斷,isInterrupted()方法便會返回true,而一旦sleep()方法拋出異常,它將清空中斷標誌,
 * 此時isInterrupted()方法將返回false
 */
public class InterruptCheck extends Object {
    public static void main(String[] args) {
        Thread thread = Thread.currentThread();
        System.out.println("[InterruptCheck] Point A: thread.isInterrupted()= " + thread.isInterrupted());
        thread.interrupt();
        System.out.println("[InterruptCheck] Point B: thread.isInterrupted()= " + thread.isInterrupted());

        try {
            Thread.sleep(2000);
            System.out.println("[InterruptCheck] was NOT interrupted");
        } catch (InterruptedException e) {
            System.out.println("[InterruptCheck] was interrupted");
        }
        System.out.println("[InterruptCheck] Point C: thread.isInterrupted()= " + thread.isInterrupted());
    }
}
/**
 * 使用Thread.interrupted()方法判斷中斷狀態(並隱式重置爲false)
 * 因爲它是靜態方法、所以不能在特定的線程上使用,而只能報告調用它的線程的中斷狀態,若是線程被中斷,
 * 並且中斷狀態尚不清楚,那麼,這個方法返回true。與isInterrupted不一樣,它將自動重置中斷狀態爲false.
 * 
 */
public class InterruptReset extends Object {
    public static void main(String[] args) {
        System.out.println("[InterruptReset] Point X: Thread.interrupted()= " + Thread.interrupted());
        Thread.currentThread().interrupt();
        System.out.println("[InterruptReset] Point Y: Thread.interrupted()= " + Thread.interrupted());
        System.out.println("[InterruptReset] Point Z: Thread.interrupted()= " + Thread.interrupted());
    }
}
相關文章
相關標籤/搜索