Java 无法停止 Process Builder 线程

标签 java multithreading processbuilder

我真的遇到了一个关于 java 线程和 ProcessBuilder 的问题。 我无法停止该线程,也许有人可以帮助我找出它不起作用的原因。我有两个类,一个 GUI 类和一个 ThreadWorker 类,其中实际线程和 ProcessBuilder 位于其中。

GUI 类:

package minimum_gui;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

    public class GUI 
    {

        private JFrame jFrame;


        ThreadWorker tw = new ThreadWorker("ThreadName");


        public GUI()
        {
            initialize();
        }

        private void initialize()
        {
            jFrame = new JFrame();
            jFrame.setTitle("Example GUI");
            jFrame.setBounds(100,100,730,330);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setResizable(true);
            jFrame.getContentPane().setLayout(null);

            JPanel panel = new JPanel();
            panel.setBackground(Color.WHITE);
            panel.setBounds(10, 11, 694, 281);
            jFrame.getContentPane().add(panel);
            panel.setLayout(null);

            JButton btnStart = new JButton("Start");
            btnStart.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    tw.start();
                }
            });
            btnStart.setBounds(28, 36, 89, 23);
            panel.add(btnStart);

            JButton btnStop = new JButton("Stop");
            btnStop.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tw.stopIt();
                }
            });
            btnStop.setBounds(28, 70, 89, 23);
            panel.add(btnStop);

        }

        public static void main(String[] args) {

            EventQueue.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    try 
                    {
                        GUI window = new GUI();
                        window.jFrame.setVisible(true);
                    } 
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                }
            });

        }
    }

还有 ThreadWorker 类:

package minimum_gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;




    public class ThreadWorker implements Runnable
    {

        Thread thread = null;
        String threadName;

        public ThreadWorker(String name)
        {
            //thread.currentThread().setName(name);
            this.threadName = name;
        }

        public synchronized void start()
        {
            if (thread == null)
            {
                thread = new Thread(this);
                thread.start();
            }
        }

        public synchronized void stopIt()
        {
            if (thread != null)
            {
                thread = null;
            }
        }

        public synchronized void interruptIt()
        {
            if (thread != null)
                thread.interrupt();
        }


        public void run() {
            while (thread != null)
            {
                try {
                    String[] command = { "CMD", "/C","ping -n 20 google.com"};
                    ProcessBuilder probuilder = new ProcessBuilder(command);


                    Process process = probuilder.start();
                    final long start = System.currentTimeMillis();
                    // Read out dir output
                    InputStream is = process.getInputStream();
                    InputStreamReader isr = new InputStreamReader(is);

                    BufferedReader br = new BufferedReader(isr);
                    String line;
                    System.out.printf("Output of running %s is :\n",
                            Arrays.toString(command));

                    try {
                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                } catch (IOException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();

                }

            }

        }
    }

最佳答案

您正在执行 20 次迭代的命令 ping ping -n 20 google.com

在这些行上打印结果:

while ((line = br.readLine()) != null) {
    System.out.println(line);
}

嗯,这个 while 只会在 ping 完成后结束,并且这个 while 不会对线程状态(null 或非 null)进行验证。

因此,如果您尝试停止工作,请在外部 while

while (thread != null)

仅当 ping 完成(20 次 ping 后)时才会停止。

要立即停止,您也需要检查内部的线程状态,例如

while ((line = br.readLine()) != null && thread != null) {
    System.out.println(line);
}

关于Java 无法停止 Process Builder 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412487/

相关文章:

java - 无法从Java调用Shell脚本

Java - 使用 SQL 语法连接列表的 API

java - GWT 开发和设计模式

java - volatile 关键字有什么用?

c - TCP服务器通过多线程向多个客户端发送和接收数据

java - 如何判断线程是否已完成?

java - 使用编码参数将 PdfToText 作为 Java 进程运行

java - 如何获取CIELAB图像a轴和b轴上的像素值

java - 如何在不使用 while 循环的情况下运行服务器?

java - keytool 命令在命令行上成功,但未通过 ProcessBuilder