黑马程序员技术交流社区

标题: 结束冻结状态的线程示例 [打印本页]

作者: 肚小糖    时间: 2015-8-24 15:29
标题: 结束冻结状态的线程示例
  1. class StopThread implements Runnable{
  2.         private boolean flag = true;
  3.         public void run(){
  4.                 while(flag){
  5.                         try{
  6.                                 wait();
  7.                         }catch(InterruptedException e){
  8.                                 System.out.println(Thread.currentThread().getName()+"..."+e);
  9.                                 flag = false;
  10.                         }
  11.                         System.out.println(Thread.currentThread().getName()+"......");
  12.                 }
  13.         }
  14.         public void setFlag(){
  15.                 flag = false;
  16.         }
  17. }
  18. public class StopThreadDemo {
  19.         public static void main(String[] args){
  20.                 StopThread st = new StopThread();
  21.                 Thread t1 = new Thread(st);
  22.                 Thread t2 = new Thread(st);
  23.                 t1.start();
  24.                 t2.start();
  25.                 int num = 1;
  26.                 for(;;){
  27.                         if(++num == 50){
  28.                                 t1.interrupt();
  29.                                 t2.interrupt();
  30.                                 break;
  31.                         }
  32.                         System.out.println("main..."+num);
  33.                 }
  34.                 System.out.println("over");
  35.         }
  36. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2