首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java 執行緒控制進階:中斷、暫停、恢復與停止模式

     編輯:Java 程式設計入門

執行緒生命週期控制方法演進

早期 Java 提供 stop()、suspend()、resume() 等強制控制方法,但因導致資料不一致、死結等嚴重問題已被淘汰。現代標準模式採用 interrupt() 中斷協作 + volatile 旗標 + wait/notify 機制實現優雅控制。

完整程式碼:threadCtrl.java(含淘汰與現代混合示範)

public class threadCtrl {
    public static void main(String[] main) {
        new threadCtrl();
    }

    threadCtrl() {
        try {
            Thread tm = Thread.currentThread(); // 主執行緒
            threadchild td = new threadchild();
            td.start();

            tm.sleep(500);
            System.out.println("interrupt child thread");
            td.interrupt(); // 1. 中斷:設置中斷旗標

            System.out.println("let child thread wait!");
            // td.wait(); // 錯誤:wait() 需在 synchronized 區塊內
            // td.suspend(); // 已淘汰
            tm.sleep(1000);

            System.out.println("let child thread working");
            td.fauxresume(); // 2. 自訂恢復:旗標 + notify
            // td.resume(); // 已淘汰
            tm.sleep(1000);

            td.runflag = false; // 3. 旗標停止:優雅結束
            System.out.println("main over..............");
        } catch (InterruptedException ie) { System.out.println("inter main::" + ie); }
        catch (Exception e) { System.out.println("main::" + e); }
    }
}

// 被控執行緒類別
class threadchild extends Thread {
    boolean runflag = true;      // 執行旗標
    boolean suspended = true;    // 暫停旗標

    threadchild() {}

    // 自訂恢復方法:旗標 + notify
    public synchronized void fauxresume() {
        suspended = false;
        notify(); // 喚醒在 wait() 中的執行緒
    }

    public void run() {
        while (runflag) {
            System.out.println("I am working..............");
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("sleep::" + e);
            }
            synchronized (this) {
                try {
                    if (suspended)
                        wait(); // 釋放鎖進入等待集
                } catch (InterruptedException e) {
                    System.out.println("wait::" + e);
                }
            }
        }
        System.out.println("thread over...........");
    }
}

關鍵控制機制對照表

控制需求淘汰方法現代標準模式核心原理
中斷執行緒stop()interrupt()設置中斷旗標,阻塞方法拋出 InterruptedException
暫停執行緒suspend()volatile 旗標 + wait()釋放鎖進入等待集,不持有鎖
恢復執行緒resume()notify()/notifyAll()喚醒等待集中執行緒
停止執行緒stop()volatile 旗標 + interrupt()迴圈檢查旗標,主動退出 run()

程式碼關鍵點解析

  1. interrupt():不強制停止,僅設置旗標;sleep()、wait() 等阻塞方法會拋出 InterruptedException 並清除旗標
  2. wait()/notify():必須在 synchronized 區塊內呼叫,wait() 釋放鎖,notify() 喚醒一個等待執行緒
  3. volatile 旗標:runflag、suspended 確保跨執行緒可見性
  4. fauxresume():自訂恢復方法,結合旗標修改與 notify()

標準優雅停止模板

public class GracefulThread extends Thread {
    private volatile boolean running = true;

    public void run() {
        while (running && !Thread.currentThread().isInterrupted()) {
            try {
                // 業務邏輯
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // 休眠被中斷,檢查旗標決定是否退出
                Thread.currentThread().interrupt(); // 重置中斷旗標(可選)
            }
        }
        // 清理資源
    }

    public void shutdown() {
        running = false;
        interrupt(); // 輔助中斷阻塞操作
    }
}

⚠️ 常見錯誤

  • td.wait() 呼叫於非同步區塊 → IllegalMonitorStateException
  • 忽略 InterruptedException 導致中斷信號丟失
  • 使用 suspend()/resume() 造成死結
  • 旗標未宣告 volatile 導致可見性問題

生產環境建議

  • 使用 ExecutorService 管理執行緒生命週期(shutdown()、shutdownNow())
  • 長時間任務定期檢查 Thread.currentThread().isInterrupted()
  • 資源清理放在 finally 或 try-with-resources
  • 避免直接操作 Thread 實例,改用高階並發工具

理解執行緒控制的演進歷程,才能寫出健壯、可維護的並發程式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved