简体   繁体   English

Java 无法停止 Process Builder 线程

[英]Java can´t stop Process Builder thread

I got really stuck with a problem concerning java threads and ProcessBuilder .我真的被一个关于 java 线程和ProcessBuilder的问题困住了。 I can´t stop the thread maybe someone could help me to figure out why it doesn´t work.我无法停止线程也许有人可以帮助我弄清楚为什么它不起作用。 I have two classes one GUI class and one ThreadWorker class where the actual thread and ProcessBuilder are.我有两个类,一个GUI类和一个ThreadWorker类,实际线程和ProcessBuilder所在的类。

GUI class: 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();
                    }
                }
            });

        }
    }

And the ThreadWorker class:和 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();

                }

            }

        }
    }

You are doing a command ping with 20 iterations ping -n 20 google.com您正在执行 ping 20 次迭代的命令ping -n 20 google.com

Printing the result on these lines:在这些行上打印结果:

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

Well, this while will only end when the ping is completed, and there is no verification about thread state (null or non-null) in this while.嗯,这个while只有在ping完成后才会结束,在这个while中没有关于线程状态(空或非空)的验证。

So if you try to stop the work, on the external while所以如果你试图停止工作,在外部而

while (thread != null)

It will only stop when ping finishes, after 20 pings.它只会在 ping 完成后停止,在 20 次 ping 之后。

To stop immediately you need to check the thread state in the inside while too, like要立即停止,您还需要检查内部的线程状态,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM