读书人

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

发布时间: 2012-01-19 20:57:59 作者: rapoo

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

读书人网 >J2SE开发

热点推荐