当前位置: 代码迷 >> J2SE >> []一个线程的简单有关问题
  详细解决方案

[]一个线程的简单有关问题

热度:61   发布时间:2016-04-24 13:45:44.0
[求助]一个线程的简单问题
为什么我用了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
  相关解决方案