怎么停止一个阻塞线城

cbuser 2007-06-27 01:11:23
第一次不输入,等终止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();
}
}
}
...全文
272 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tdy1234 2007-06-29
  • 打赏
  • 举报
回复
晕joejoe1991() ( ) 好强的方法 佩服佩服
cbuser 2007-06-28
  • 打赏
  • 举报
回复
how to?
joejoe1991 2007-06-28
  • 打赏
  • 举报
回复
把流关掉,。 让程序发生异常而离开run方法。。 这样线程应该就结束了吧?
tdy1234 2007-06-28
  • 打赏
  • 举报
回复
搞不定 只好不看了呵呵
cbuser 2007-06-28
  • 打赏
  • 举报
回复
人呢?
cbuser 2007-06-27
  • 打赏
  • 举报
回复
If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException
照这段话,调用被阻塞thread.interrupt()应该还是能够起作用,并会抛出interrupt异常啊?
cbuser 2007-06-27
  • 打赏
  • 举报
回复
不好意思两种方式都不行,如果不休眠,都不知道a执行到哪一步了,如果是在system.in前面杀掉,没有什么意义;setDaemon加不加都一样
angelseyes 2007-06-27
  • 打赏
  • 举报
回复
//a.setDaemon(true);
tdy1234 2007-06-27
  • 打赏
  • 举报
回复
Thread.sleep(10000); 把这句去掉
cbuser 2007-06-27
  • 打赏
  • 举报
回复
线程尚未终止
tdy1234 2007-06-27
  • 打赏
  • 举报
回复
public static void main(String[] args)
{
AThread a=new AThread();
a.setDaemon(true);
a.start();
try{
Thread.sleep(10000);
a.interrupt();
if(!a.interrupted() ){System.out.println("线程尚未终止");}\\加一句测试一下看看
}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!");
}
}
cbuser 2007-06-27
  • 打赏
  • 举报
回复
不是推不推荐使用的问题,是使用了也没效果
tdy1234 2007-06-27
  • 打赏
  • 举报
回复
stop 好像会造成死锁不推荐使用
cbuser 2007-06-27
  • 打赏
  • 举报
回复
不行的,stop,interrupt,yield都用上都不行;(
polarman 2007-06-27
  • 打赏
  • 举报
回复
试试把a.interrupt()改成a.stop()看看行不行

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧