杀死线程关闭

时间:2014-06-22 20:07:18

标签: java multithreading jpanel

我有一个JFrame的构造函数,我有一个Thread(t1)正在运行,感谢

    while(true)

我想知道如何实现我的JFrame,以便在关闭它时可以杀死线程,因为当JFrame处于活动状态时需要运行t1

编辑: 这是代码:

public class Vue_Session extends JFrame {
private  JPanel contentPane;
private int idsess;
private User u;

public Vue_Session(User us, int id) {
    this.u = us;
    this.idsess = id;
    toServ t=new toServ(idsess);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((int) screenSize.getWidth() / 2 - 800 + (800 / 2), 90, 800,
            600);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(new BorderLayout());

    Vue_Idee vueIdee = new Vue_Idee(this.idsess, this.u);
    contentPane.add(vueIdee, BorderLayout.SOUTH);
    Vue_IdeeSession vueSess = new Vue_IdeeSession(this.idsess);
    contentPane.add(vueSess, BorderLayout.CENTER);
    Thread t1 = new Thread( new Runnable(){
        public void run(){
            while(true){
                try{
                    Thread.sleep(500);
                }catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                t.getIdee();
                vueSess.act();
                revalidate();
            }
        }
    });
    t1.start();
}

4 个答案:

答案 0 :(得分:0)

这是一个有争议的话题,但总的来说我会用

替换while (true)构造
       while(!Thread.currentThread().isInterrupted()){
            try{
                Thread.sleep(500);
            }catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            t.getIdee();
            vueSess.act();
            revalidate();
        }

有关此主题的更多信息,请访问:

http://www.javaspecialists.eu/archive/Issue056.html

答案 1 :(得分:0)

您可以使用类似

的布尔变量来处理它
boolean end = false;
while (!end){...}

此外,我建议您使用 ExecutorService ForkJoinPool ,以便您可以简单地控制您的任务,线程等

编辑:

boolean end = false;

new Thread(() -> {
    while (!end) {
        //...
    }
}).start();

这是您应该结束任务的地方:

addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosed(java.awt.event.WindowEvent evt) {
        end = true;
        System.exit(0);
        // or this.dispose();
    }
});
祝你好运:)

答案 2 :(得分:0)

好的,这是答案: 我需要添加en WindowListenne:

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent b) {

        t1.stop();
        dispose();

    }

});

还有:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

答案 3 :(得分:0)

首先,你需要让线程杀死。要做到这一点,只要某个循环标志为真,就不要循环,而是循环。

之后,您需要创建一个在用户关闭框架时调用的侦听器。您可以使用WindowAdapter执行此操作。调用侦听器时,将循环标志设置为false。一旦线程死亡,您就可以安全地终止该程序。

例如:

public class Vue_Session extends JFrame {

    Thread thread = null;
    boolean threadAlvie = true;
    boolean threadDie = false;

    public Vue_Session(User us, int id) {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                threadAlive = false;
                // Wait until the thread dies
                while (!threadDie) {
                  // Sleep for 100 milliseconds.
                  Thread.sleep(100);
                }
                System.exit(0);
            }
        });
        thread = new Thread(new Runnable() {
            public void run(){
                while (threadAlive){
                  // do something
                }
                threadDie = true;
            }
        });
        thread.start();
    }
}