【发布时间】:2015-02-21 09:50:40
【问题描述】:
当我运行这段代码时,它会显示以下输出:
One : 15
Two : 15
One : 14
Two : 14
Two : 13
One : 13
Two : 12
One : 12
One : 11
Two : 11
Thread 1 suspended
Two : 10
Two : 9
Two : 8
Two : 7
Two : 6
Thread 1 resumed
Thread 2 suspended
Thread 2 resumed
输出不会持续到最后 一:1 二:1 NewThread1类的myresume方法没有执行吗?这背后的原因是什么?
下面是NewThread1的代码:
class NewThread1 implements Runnable{
String name;
Thread t;
boolean suspendFlag;
NewThread1(String threadname){
name = threadname;
t = new Thread(this, name);
suspendFlag = false;
t.start();
}
@Override
public void run(){
try{
for(int i=15; i>0; i--){
System.out.println(name+ " : " +i);
Thread.sleep(200);
synchronized(this){
while(suspendFlag){
wait();
}
}
}
}catch(InterruptedException e){
System.out.println("New thread1 Interrupted");
}
}
synchronized void myresume(){
suspendFlag = false;
}
void mysuspend(){
suspendFlag = true;
}
}
下面是NewThread1的代码:(这里定义了main()方法)
public class Suspend_ResumeThreads {
public static void main(String args[]){
NewThread1 ob1 = new NewThread1("One ");
NewThread1 ob2 = new NewThread1("Two ");
try{
Thread.sleep(1000);
ob1.mysuspend();
System.out.println("Thread 1 suspended");
Thread.sleep(1000);
ob1.myresume();
System.out.println("Thread 1 resumed");
ob2.mysuspend();
System.out.println("Thread 2 suspended");
Thread.sleep(1000);
ob2.myresume();
System.out.println("Thread 2 resumed");
}catch(InterruptedException e){
System.out.println("Main Interrupted");
}
try{
ob1.t.join();
ob2.t.join();
}catch(InterruptedException e){
System.out.println("Main interrupeted in join()");
}
System.out.println("Main exiting..");
}
}
【问题讨论】:
-
您必须在同一个对象上调用
.notify()才能唤醒卡在该对象的.wait()中的线程。 -
当您使用通知/等待时,这应该始终与状态更改相关联。 Notify() 可以在没有任何等待的情况下发生,这将丢失,并且 wait() 可以虚假唤醒。
-
还将
suspendFlag标记为易失性。或在synchronized部分更改它
标签: java multithreading resume suspend thread-synchronization