簡體   English   中英

Java中的暫停和恢復線程不起作用

[英]Pause and resume thread in java is not working

我想分別使用wait()/ notify()應用暫停/恢復線程。 我已反復嘗試解決此問題,但無法解決,因此請幫助我並解釋為什么notify();。 不會使計數器線程可運行:

public class Suspend {
boolean isSuspend = false;
int counter = 0;

synchronized public void suspend() {    
    isSuspend = true;
    System.out.println("The counter was suspended!");
}

synchronized public void resume() {
    isSuspend = false;
    System.out.println("The counter was resumed :)");
    notify();
}

public static void main(String[] args) {
    Thread.currentThread().setName("Main Thread");
    Suspend suspend = new Suspend();

    Thread counterThread = new Thread(new Runnable() {
        synchronized public void run() {
            while(!suspend.isSuspend) {
                System.out.println(suspend.counter++);
                try { Thread.sleep(1000); }
                catch (InterruptedException e) {}
            }
            try {
                while(suspend.isSuspend)
                    wait();
                }
            catch (InterruptedException e) {}
        }
    }, "Counter Thread");

    Thread suspendResumeThread = new Thread(new Runnable() {
        synchronized public void run() {
            while(true) {
                try {
                    Thread.sleep(5000);
                    suspend.suspend();
                    Thread.sleep(5000);
                    suspend.resume();
                } catch (InterruptedException e) {}
            }
        }
    }, "Suspend/Resume Thread");

    counterThread.start();
    suspendResumeThread.start();
}

}

輸出如下: 0 1 2 3 4 The counter was suspended! The counter was resumed :) The counter was suspended! The counter was resumed :) ... and so on. 0 1 2 3 4 The counter was suspended! The counter was resumed :) The counter was suspended! The counter was resumed :) ... and so on.

問題是這些行:

while (suspend.isSuspend)
    wait();
}

在您的counterThread Runnable中。

您在Runnable上等待,而不是在suspend對象上等待

你需要同步的suspend和呼吁的wait() suspend

synchronized (suspend) {
    while (suspend.isSuspend)
        suspend.wait();
    }
}

另外,您的run方法不需要同步。

看一下counterThread,它在isSuspend標志的第一次更改時結束。

我認為您需要這樣的東西:

public class Suspend {
volatile boolean isSuspend = false;
int counter = 0;

synchronized public void suspend() {    
    isSuspend = true;
    System.out.println("The counter was suspended!");
}

synchronized public void resume() {
    isSuspend = false;
    System.out.println("The counter was resumed :)");
    notify();
}

public static void main(String[] args) {
    Thread.currentThread().setName("Main Thread");
    Suspend suspend = new Suspend();

    Thread counterThread = new Thread(new Runnable() {
        synchronized public void run() {
            while(true){
                while(!suspend.isSuspend) {
                    System.out.println(suspend.counter++);
                    try { Thread.sleep(1000); }
                    catch (InterruptedException e) {}
                }
                try {
                    while(suspend.isSuspend)
                        wait();
                }catch (InterruptedException e) {}
                }
             }
         }
    }, "Counter Thread");

    Thread suspendResumeThread = new Thread(new Runnable() {
        synchronized public void run() {
            while(true) {
                try {
                    Thread.sleep(5000);
                    suspend.suspend();
                    Thread.sleep(5000);
                    suspend.resume();
                } catch (InterruptedException e) {}
            }
        }
    }, "Suspend/Resume Thread");

    counterThread.start();
    suspendResumeThread.start();
}

最后,您還需要在共享變量上使用volatile關鍵字

編輯:我粘貼了錯誤的代碼對不起

暫無
暫無

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

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