码迷,mamicode.com
首页 > 其他好文 > 详细

例8.13 本例用于演示挂起和恢复!

时间:2016-05-17 11:37:47      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

// 本例用于演示挂起和恢复!
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();
}

}

例8.13 本例用于演示挂起和恢复!

标签:

原文地址:http://www.cnblogs.com/sxdxjava/p/5500746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!