gpt4 book ai didi

java - 完成执行服务不工作

转载 作者:行者123 更新时间:2023-12-01 14:03:50 25 4
gpt4 key购买 nike

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

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 仍在进程列表中,我必须手动完成它,或者有办法自动终止附加到可运行的进程?

最佳答案

将进程分配给成员变量,然后在单击停止按钮时调用其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);

}

关于java - 完成执行服务不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com