A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Fightingforever 中级黑马   /  2014-10-24 23:01  /  1101 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class StopThread implements Runnable
  2. {
  3.         private boolean flag=true;
  4.         public synchronized void run()
  5.         {
  6.                 while(flag)
  7.                                 try
  8.                                 {
  9.                                         this.wait();
  10.                                         }
  11.                                 catch(InterruptedException e)
  12.                                 {
  13.                                         System.out.println(Thread.currentThread().getName()+"...Exception");
  14.                                         }
  15.                                 System.out.println(Thread.currentThread().getName()+"...run");
  16.                 }
  17.                
  18.                 public void changeFlag()
  19.                 {
  20.                         flag=false;
  21.                         }
  22.         }
  23.        
  24.        
  25. class StopThreadDemo
  26. {
  27.         public static void main(String[] args)
  28.         {
  29.                 StopThread st=new StopThread();
  30.                
  31.                 Thread t1=new Thread(st);
  32.                 Thread t2=new Thread(st);
  33.                
  34.                 t1.start();
  35.                 t2.start();
  36.                
  37.                 int num=0;
  38.                
  39.                 while(true)
  40.                 {
  41.                         if(num++==60)
  42.                         {
  43.                                 st.changeFlag();
  44.                                 break;
  45.                                 }
  46.                         System.out.println(Thread.currentThread().getName()+"......"+num);
  47.                         }
  48.                 }
  49.         }

  50. 有谁能帮忙分析一下这段代码
复制代码

1 个回复

倒序浏览
  1. class StopThread implements Runnable
  2. {
  3.         private boolean flag=true;      //定义线程结束标记
  4.         public synchronized void run()   //将整个run方法定义为同步方法
  5.         {
  6.                 while(flag)          //如果标记为真,继续循环。否则结束线程。
  7.                                 try
  8.                                 {
  9.                                         this.wait();       //线程启动后,执行wait方法,进入冻结状态。
  10.                                         }
  11.                                 catch(InterruptedException e)      
  12.                                 {
  13.                                         System.out.println(Thread.currentThread().getName()+"...Exception");
  14.                                         }
  15.                                 System.out.println(Thread.currentThread().getName()+"...run");
  16.                 }
  17.                
  18.                 public void changeFlag()     //对外提供的结束线程方法:将线程结束标记置为false
  19.                 {
  20.                         flag=false;
  21.                 }
  22.         }
  23.         
  24.         
  25. class StopThreadDemo
  26. {
  27.         public static void main(String[] args)   //主函数
  28.         {
  29.                 StopThread st=new StopThread();    //新建对象
  30.                
  31.                 Thread t1=new Thread(st);        //新建两个线程对象并启动
  32.                 Thread t2=new Thread(st);
  33.                
  34.                 t1.start();
  35.                 t2.start();
  36.                
  37.                 int num=0;
  38.                
  39.                 while(true)
  40.                 {
  41.                         if(num++==60)          //如果循环次数达到60,便执行线程的结束方法,将flag标记为false。并跳出循环。
  42.                         {
  43.                                 st.changeFlag();
  44.                                 break;
  45.                                 }
  46.                         System.out.println(Thread.currentThread().getName()+"......"+num);
  47.                         }
  48.                 }
  49.         }
复制代码


在主线程中执行线程的changeFlag方法并不能结束两个线程。因为两个线程启动后便进入冻结状态,不能检测到线程结束标记flag。此种情况应在主线程中先执行changeFlag方法,再执行两个线程的interrupt方法,使线程解除冻结状态,继续循环,然后检测到线程结束标记并结束。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马