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

[求助]一个线程的简单问题
为什么我用了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();
}

}

------解决方案--------------------
发送中断信号 不表示马上就中止线程
一般要中止线程,最好在线程内部用return