日期:2014-05-20  浏览次数:20655 次

怎么停止一个阻塞线城
第一次不输入,等终止athread,但是实际上它还是活的,还在等待键盘输入,然后第二次输入要两次回车才能取到数据,该怎么把第一个线程彻底杀死,不让用户需要输入两次回车
import   java.io.*;
public   class   Test  
{

public   static   void   main(String[]   args)  
{
AThread   a=new   AThread();
a.setDaemon(true);      
a.start();
try{
Thread.sleep(10000);
a.interrupt();

}catch(Exception   er){}
try{ Thread.sleep(10000);}catch(Exception   er2){}
System.out.println(a.isAlive());
a=null;
try{ Thread.sleep(10000);}catch(Exception   er2){}
a=new   AThread();
a.start();
System.out.println( "Hello   World! ");
}
}
class   AThread   extends   Thread
{
      public     void   run()     {
      try{
System.out.println( "start   Thread   !\n   输入数据 ");
BufferedReader   in   =   new   BufferedReader(   new   InputStreamReader(   System.in   )   )   ;
String   str   =   in.readLine()   ;
System.out.println( "输入数据 "+str);
      }catch(Exception   err){
        err.printStackTrace();
      }
}
}

------解决方案--------------------
试试把a.interrupt()改成a.stop()看看行不行
------解决方案--------------------
stop 好像会造成死锁不推荐使用
------解决方案--------------------
//a.setDaemon(true);
------解决方案--------------------
把流关掉,。 让程序发生异常而离开run方法。。 这样线程应该就结束了吧?