首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

如何停止一个阻塞线城

2012-03-12 
怎么停止一个阻塞线城第一次不输入,等终止athread,但是实际上它还是活的,还在等待键盘输入,然后第二次输入

怎么停止一个阻塞线城
第一次不输入,等终止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方法。。 这样线程应该就结束了吧?

热点排行