java - 如何用 swing 杀死一个线程?

标签 java multithreading swing

我用 swing ui 创建了一个 java 程序。首先,我有一个问题,当我点击一个按钮时我无法点击另一个按钮。我应该等待这个人完成他的任务。所以我创建了一个全局变量线程,并在 Action 事件中线程执行该方法。如以下代码所示

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

        t = new Thread() {
            private int postion;

            public void run() {
                InstallApk installapk = new InstallApk();
                int position = 0;
                String fileName = directory;
                String shellCommand = fileName;

                // for (int position =0; postion < 105;position +5) {
                jProgressBar1.setValue(InstallApk.value);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }
                position += 5;
                // }

                jProgressBar1.setIndeterminate(true);

                installapk.execShellCmd(shellCommand);
                System.out.println("la valeur d'execution " + InstallApk.value);

                if (InstallApk.value > 3) {
                    jProgressBar1.setIndeterminate(false);
                }
            }

        };
        pid = t.getId();
        t.start();
    } 

我想通过使用另一个按钮来停止该线程的执行。我试过 t.stop(); t.destroy(); kill pid 但一直有一些结果我无法停止执行。 在执行我的方法 execShellCmd 时,我使用了一个 swing worker,但我的问题仍然是如何停止执行。

public static void execShellCmd(String cmd) {
        if (runningProcess != null) {
            // TODO print some suitable warning about process already running
            return;
        }
        try {

            //testPath = getPath();

            System.out.println("le chemin de travail.." +testPath);
            System.out.println(cmd);
            Runtime runtime = Runtime.getRuntime();
            process = runtime.exec(new String[] { "sh",
                    testPath + "/install.sh", cmd });
            new SwingWorker<Integer, Void>() {
                protected Integer doInBackground() {
                    // read process output first
                    BufferedReader buf = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));
                    String line = "";
                    try {
                        while ((line = buf.readLine()) != null) {
                            System.out.println("exec response: " + line);
                            log = line;
                            writeToFile(line);
                            value ++;
                        }
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                     System.out.println("la valeur d'execution "+InstallApk.value);
                    // only once we've run out of output can we call waitFor
                    while (true) {
                        try {
                            return runningProcess.waitFor();
                        } catch (InterruptedException e) {
                            // do nothing, wait again
                        } finally {
                            Thread.interrupted();
                        }
                    }
                }
                protected void done() {
                    runningProcess = null;
                }
            }.execute();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

最佳答案

根据您提供的代码,您可能希望通读 Swing 中的线程,因为听起来问题似乎是由于不了解线程模型的工作原理而引起的。

本质上,您有一个事件分派(dispatch)线程,所有 Swing 操作都应该在该线程上调用(如果您还没有在该线程上使用 SwingUtilities.invokeLater() EDT。)但是,根本不应在 EDT 上执行长时间运行的任务,否则(如您所见)它会锁定 GUI 直到它们完成。

在您想要执行长时间运行的任务的情况下,您的路线是正确的,但您通常会使用 SwingWorker ,它允许异步执行长时间运行的任务,还可以在任务完成时在 EDT 上提供回调。

就停止这样的线程而言,一种简单的方法是让它在执行时监视一个字段,并在您希望它停止时从 EDT 切换该字段(一个简单的操作)。不要使用 stop() 方法或类似的方法,它们有很多问题(它们被弃用是有充分理由的。)如果您使用的是 SwingWorker 那么它内置取消支持,使用起来很明智。

关于java - 如何用 swing 杀死一个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15244425/

相关文章:

java - 如何在 JFrame 中的另一个 JPanel 之上显示/隐藏 JPanel?

java - 当在客户端 GWT 模块中调用时,System.out.println() 消息去哪里了?

java - 如何调度 Java 线程

java - 从运行方法/线程返回数据以更新 GUI

java - 打印页面宽度大于高度的 jpanel 数据

java - 在Java中从JTable创建pdf文件

java - 打包应用程序时获取包 org.springframework.transaction.annotation 不存在错误

java - 制作不同对象的 Java 列表

java - 消息聊天不太好用

c++ - boost::asio 中的 post 和 dispatch 有什么区别?