例8.13 本例用于演示挂起和恢复!
// 本例用于演示挂起和恢复!
class E13
{
public static void main(String args[])
{
A a=new A();
Thread thread=new Thread(a);
thread.setName("zhang san");
thread.start();
while(a.getStop()==false){}
System.out.println("我是主线程,负责恢复"+thread.getName()+"线程");
a.restart();
}
}
class A implements Runnable
{
int number=0;
boolean stop=false;
boolean getStop()
{
return stop;
}
public void run()
{
while(true)
{
number++;
System.out.println(Thread.currentThread().getName()+"的number="+number);
if(number==3){
try{
stop=true;
hangUP();
System.out.println(Thread.currentThread().getName()+"恢复");
}catch(Exception e){}
number=0;
}
try{ Thread.sleep(1000);}
catch(Exception e){}
}
}
synchronized void hangUP() throws InterruptedException{
wait();
}
synchronized void restart(){
notifyAll();
}
}