0

我使用堆栈中一个人的代码:

package test;

import java.awt.EventQueue;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Pinger {

    /**
     * @param args
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                Pinger pinger = new Pinger();
                JFrame frame = new JFrame();
                JPanel panel = new JPanel();
                frame.setSize(300, 400);
                frame.add(panel);
                JTextArea textArea = new JTextArea();
                panel.add(textArea);
                frame.setVisible(true);
                pinger.executor.execute(pinger.createRunnable("google.com",
                        textArea));
            }
        });

    }

    private Runnable createRunnable(final String ip, final JTextArea area) {
        return new Runnable() {
            public void run() {
                String resposta = "";
                String comando = "ping -t " + ip;

                try {
                    Scanner S = new Scanner(Runtime.getRuntime().exec(comando)
                            .getInputStream());
                    while (S.hasNextLine()) {
                        final String newText = S.nextLine();
                        EventQueue.invokeLater(new Runnable() {
                            public void run() {
                                area.setText(area.getText()
                                        + System.getProperty("line.separator")
                                        + newText);
                            }
                        });

                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        };
    }

    Executor executor = Executors.newFixedThreadPool(1);

}

它工作完美,对接

pinger.executor.shutdown(); 不工作,textArea 仍然得到 ping 响应,并且 PING.exe、conhost.exe 仍在进程列表中,我必须手动完成它或者有一种方法可以自动终止附加到可运行的进程?

4

1 回答 1

1

将进程分配给成员变量,然后destroy在单击停止按钮时调用其方法。

public class Pinger {

    protected Process process;

    /**
     * @param args
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                final Pinger pinger = new Pinger(); // Pinger must be final for access by anonymous class below
                JFrame frame = new JFrame();
                JPanel panel = new JPanel();
                frame.setSize(300, 400);
                frame.add(panel);
                JTextArea textArea = new JTextArea();

                // Create the stop button and handle the client event
                JButton stop = new JButton();
                stop.setText("Stop");
                stop.addMouseListener(new MouseListener() {
                    public void mouseClicked(MouseEvent arg0) {
                        try {
                            pinger.process.destroy();
                        } catch (Exception e){}                        
                    }
                    public void mouseReleased(MouseEvent arg0) {}
                    public void mousePressed(MouseEvent arg0) {}
                    public void mouseExited(MouseEvent arg0) {}
                    public void mouseEntered(MouseEvent arg0) {}
                });
                panel.add(stop);
                panel.add(textArea);


                frame.setVisible(true);
                pinger.executor.execute(pinger.createRunnable("google.com",
                        textArea));
            }
        });

    }

    private Runnable createRunnable(final String ip, final JTextArea area) {
        return new Runnable() {
            public void run() {
            String resposta = "";
            String comando = "ping -t " + ip;

            try {
                // assign the new process object to your instance variable
                process = Runtime.getRuntime().exec(comando);
                Scanner S = new Scanner(process.getInputStream());
                while (S.hasNextLine()) {
                    final String newText = S.nextLine();
                    EventQueue.invokeLater(new Runnable() {
                        public void run() {
                            area.setText(area.getText()
                                    + System.getProperty("line.separator")
                                    + newText);
                        }
                    });

                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
    };
}

Executor executor = Executors.newFixedThreadPool(1);

}
于 2013-09-30T19:10:47.687 回答