[求助]一个线程的简单问题

chenyuelin311 2007-09-14 11:31:27
为什么我用了interrupt之后线程不会停止,API上说可以中断线程的啊。代码如下:

public class dfdfdfdf extends JFrame {

dfdfdfdf(){
final Thread th=new Thread(new Runnable(){
public void run(){
while(true){
System.out.println("*");
}
}
});
JButton b=new JButton("OK");
add(b);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
th.interrupt();
}
});
th.start();
}

public static void main(String[] args) {
new dfdfdfdf();
}

}
...全文
129 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangkuisky 2008-01-18
  • 打赏
  • 举报
回复

dfdfdfdf() 太不规范了
beiouwolf 2007-09-14
  • 打赏
  • 举报
回复
发送中断信号 不表示马上就中止线程
一般要中止线程,最好在线程内部用return

62,623

社区成员

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

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