java中的暂停和恢复线程不工作
我想分别使用wait()/notify()应用挂起/恢复线程。 我已多次尝试解决此问题,但无法解决,请帮助我并解释为什么通知();不使计数器线程可运行:java中的暂停和恢复线程不工作,java,eclipse,multithreading,Java,Eclipse,Multithreading,我想分别使用wait()/notify()应用挂起/恢复线程。 我已多次尝试解决此问题,但无法解决,请帮助我并解释为什么通知();不使计数器线程可运行: public class Suspend { boolean isSuspend = false; int counter = 0; synchronized public void suspend() { isSuspend = true; System.out.println("The counter was su
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.
柜台被暂停了!
柜台恢复营业:)
柜台被暂停了!
柜台恢复营业:)
... 以此类推。
查看counterThread,它在第一次更改issuplose标志时结束
我想你需要这样的东西:
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
关键字
编辑:我粘贴了错误的代码抱歉问题在于这些行:
while (suspend.isSuspend)
wait();
}
在您的反向线程中
可运行
您等待的是可运行
,而不是挂起
对象
您需要在suspend
上同步,并在suspend
上调用wait():
synchronized (suspend) {
while (suspend.isSuspend)
suspend.wait();
}
}
另外,您的
运行方法不需要同步。boolean=false
可能是易失性的,或者使用AtomicBoolean
而不是第一个线程等待一秒钟,这样它会打印5次,然后第二个线程挂起“锁”,这样第一个线程循环就会中断。由于您只是在notify()之后调用wait(),代码将恢复,但之后没有代码可执行,正确的做法是在while循环中调用wait(),并将其更改为while(true),其中包含一些中断条件。@谢谢您的评论,我尝试了volatile和AtomicBoolean,但不幸的是,它不起作用。@MarcosVasconcelos谢谢你的评论,我尝试了以下方法,但不起作用:while(true){try{if(!suspend.issupspend){System.out.println(suspend.counter++);try{Thread.sleep(1000);}catch(InterruptedException e){}else wait();}catch(InterruptedException e){}感谢您的重要提示:*您还需要在共享变量**上使用volatile关键字。但是你的代码不起作用,计数器线程也没有结束。根据调试器,计数器线程的状态是“等待:暂停$1(id=19)”真的,我没有注意到Suspend对象上缺少syncronized,但无论如何你需要额外的while(真的)非常感谢Johannes Kuhn先生,实际上,您的代码是100%正确的。while
循环必须在同步块内,否则这不是线程安全的。wait()
的要点在于它是一个原子操作,但只有在锁被持有时才可以。实际上,如果suspend.issspended
是volatile
,则它不需要同步。