public class Test{
public static void main(String args[]){
MyThread thread=new MyThread();
thread.setName("张三");
thread.start();
while(thread.getStop()==false) {}
System.out.println("我是主线程,负责恢复"+thread.getName()+"线程");
thread.restart(); //恢复thread线程
}
}
class MyThread extends Thread{
int number=0;
boolean stop=false;
boolean getStop(){
return stop;
}
public synchronized void run(){
while(true){
number++;
System.out.println(Thread.currentThread().getName()+"的number="+number);
if(number==3){
try{ System.out.println(Thread.currentThread().getName()+"被挂起");
stop=true;
hangUP();//挂起线程
System.out.println(Thread.currentThread().getName()+"恢复执行");
}
catch(Exception e){}
}
try{ Thread.sleep(1000);
}
catch(Exception e){}
}
}
public synchronized void hangUP() throws InterruptedException{
wait();
}
public synchronized void restart(){
notifyAll();
}
}
为啥主函数里面死循环无法获取已经更改的thread.getStop()

java多线程中死循环问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-