簡體   English   中英

如何使用Java Thread.currentThread()。interrupt()

[英]How to use java Thread.currentThread().interrupt()

如果我將運行功能覆蓋為,

Thread t = new Thread(){
                public void run(){
                    try {
                        if(Thread.currentThread().isInterrupted()) {
                            doSomePrcocess() // is the isInerrupted() flag seeting to true?
                            return; //Terminates the current Thread
                        }
                        //otherwise 
                        runScript();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();

然后,如果我從代碼中的任何點調用Thread.currentThread().interrupt() ,線程是否應該在那里停止並在該點開始運行doSomeProcess()? 如果是,那么中斷標志如何設置為true? 如果沒有,該怎么做?

  • 如果線程處於睡眠或等待狀態,則在線程上調用interrupt()方法,將中斷睡眠或等待狀態

拋出InterruptedException

  • 如果線程未處於睡眠或等待狀態,則調用interrupt()方法將執行正常行為,並且不會中斷線程,而是將中斷標志設置為true。

線程類具有處理線程中斷的規定

public void interrupt()
public static boolean interrupted()
public boolean isInterrupted()

如果打算只執行一次doSomePrcocess,則必須繼續執行,它將檢查並清除連續調用的線程中斷狀態。

public static boolean interrupted()

在下面使用只會檢查狀態,不會進行任何修改。

public boolean isInterrupted()

我在下面的代碼中有一個帶有注釋的運行示例。 嘗試運行幾次以查看它是否可以闡明您的概念。

通常,您將從另一個線程中中斷一個線程,是的,doSomeProcess將在循環的下一個周期中調用,該周期可能是該線程被中斷后1 ms,也可能是該方法中實現的邏輯后1小時。

public class InterruptTest {

    public static void main(String[] args) throws InterruptedException {

        Thread t = new Thread() {
            public void run() {
                while (true) {
                    try {

                        if (Thread.currentThread().isInterrupted()) {
                            doSomePrcocess(); // is the isInerrupted() flag seeting to true? - Yes
                            return; // Terminates the current Thread - yes
                        }
                        // otherwise
                        runScript();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            private void runScript() {
                System.out.println("runScript interrupted status:" + this.isInterrupted());
                sleepy(100);


            }

            private void doSomePrcocess() {
                System.out.println("doSomePrcocess interrupted status:" + this.isInterrupted());
                sleepy(500);
            }

            private void sleepy(int millis) {
                try {
                    Thread.sleep(millis);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Thread.currentThread().interrupt(); // try commenting this out to see what happens.
                }
            }
        };
        t.start(); 

        Thread.sleep(1000);

        t.interrupt(); // generally you would call interrupt on another thread.

    }

}

不,它不是那樣工作的。 isInterrupted方法檢查是否設置了標志,它沒有聲明處理程序。 沒有辦法定義一個中央處理程序,當一個線程被中斷時它將自動被調用。 您可以做的是捕獲InterruptedException並調用處理程序,並定期檢查中斷標志以查看是否該停止了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM