简体   繁体   English

Java中的暂停和恢复线程不起作用

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

I wanted to apply suspend/resume threads using wait()/notify() respectively. 我想分别使用wait()/ notify()应用暂停/恢复线程。 I've tried repeatedly to solve this problem but I couldn't, so please kindly help me and explain why notify(); 我已反复尝试解决此问题,但无法解决,因此请帮助我并解释为什么notify();。 doesn't make counter thread runnable: 不会使计数器线程可运行:

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();
}

} }

The output is as following: 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. 0 1 2 3 4 The counter was suspended! The counter was resumed :) The counter was suspended! The counter was resumed :) ... and so on.

The problem are those lines: 问题是这些行:

while (suspend.isSuspend)
    wait();
}

in your counterThread Runnable. 在您的counterThread Runnable中。

You wait on the Runnable , not on the suspend object 您在Runnable上等待,而不是在suspend对象上等待

You need to synchronize on suspend and call wait() on suspend : 你需要同步的suspend和呼吁的wait() suspend

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

Also, your run methods don't need to be synchronized. 另外,您的run方法不需要同步。

Take a look to counterThread while, it end at first change of isSuspend flag. 看一下counterThread,它在isSuspend标志的第一次更改时结束。

I think you need something like this: 我认为您需要这样的东西:

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();
}

In the end you need to use volatile keyword too on shared variables 最后,您还需要在共享变量上使用volatile关键字

EDIT: i've pasted the wrong code sorry 编辑:我粘贴了错误的代码对不起

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM