关于Java:如何使用Swing杀死线程?

how to kill a thread with swing?

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

中所述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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();杀死PID,但一直以来,某些结果我无法停止执行。
在我的方法execShellCmd的实现中,我使用了swing工人,但我的问题仍然是如何停止执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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中的线程,因为听起来好像是由于对线程模型的工作原理了解不足而引起的问题。

从本质上讲,您有一个事件调度线程,应该调用所有的Swing操作(如果尚未在此EDT上,请使用SwingUtilities.invokeLater())。但是,长时间运行的任务不应在EDT上执行全部,否则(如您所见)它将锁定GUI,直到完成。

在要执行长时间运行的任务的情况下,您的做法是正确的,但通常使用SwingWorker,它允许异步执行长时间运行的任务,但还提供了回调任务完成后在EDT上显示

就停止这样的线程而言,一种简单的方法是让它在执行时监视某个字段,并在您希望其停止时从EDT(微不足道的操作)中切换该字段。不要使用stop()方法,或者同样,它们有很多问题(出于充分原因不建议使用。)如果您使用的是SwingWorker,则它具有取消内置的支持,这是明智的选择使用。