gpt4 book ai didi

java - 如果线程尚未完成,则暂停进一步执行

转载 作者:行者123 更新时间:2023-12-02 10:07:17 25 4
gpt4 key购买 nike

我编写了一个程序,它执行 cmd 命令并将输出打印到程序的“控制台”。我已经使用 Thread 打印输出而不卡住程序。我希望能够看到实时输出。

它的问题在于,在调用 executeCommand 方法之后的 initialize 部分是在executeCommand 之后立即执行的。我想要做的是一旦线程停止运行就执行初始化的其余部分。如果不卡住整个程序我就无法做到这一点。

我使用了线程连接方法和类似方法,但我的应用程序完全卡住了。

这是我的主课

private String genCmd2;

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ConvertGUI window = new ConvertGUI();
window.frmConvert.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public ConvertGUI() {
initialize();
}

private void initialize() {
// Execute a generated command concurrently
genCmd2 = "ping google.com -n 5";
executeCommand(genCmd2, genCmdTextArea);

//CODE TO RUN AFTER EXECUTE COMMAND IS FULLY FINISHED
//NOT RIGHT AFTER executeCommand method is called

}

public static void printToTheConsole(JTextArea console, String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
console.append(message);
console.validate();
}
});
}

private static void executeCommand(String command, JTextArea console) {
Runnable r = new CommandLineRunnable(command, console);
t = new Thread(r);
t.start();
}

我的 Runnable 类执行命令并将内容打印到控制台

public class CommandLineRunnable extends ConvertGUI implements Runnable {
private String generatedCommand;
private JTextArea console;

public CommandLineRunnable(String command, JTextArea console) {
this.generatedCommand = command;
this.console = console;
printToTheConsole(console, command);
}

@Override
public void run() {
StringBuilder output = new StringBuilder();
BufferedReader reader;
Process process;
try {
process = Runtime.getRuntime().exec(generatedCommand);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;

while ((line = reader.readLine()) != null) {
output.append(line + "\n");
printToTheConsole(console, line + "\n");
}

printToTheConsole(console, "\n--------------------------Success!--------------------------\n");

reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

最佳答案

如果您想打印到控制台,并在您的 Runnable# 任务之一(未)成功执行期间和之后更新 JTextArea,您可以实现一个接口(interface)并将其提供给您的 Runnables 构造函数

考虑以下示例,该示例使用参数 String 'aCommand' 作为命令、fa JTextArea 对象 'console' 和一个新的 ResponseEvent 匿名类作为其参数来实例化 CommandLineRunnable 类。

请注意,由于匿名类中的重复,如果您正在执行多个命令,您可能不想多次实例化匿名类,而只需将 printToTheConsole 代码插入到功能接口(interface)的方法中

    public static void main(String[] args) {

JTextArea console = new JTextArea();

/**
* JTextArea init code here
*/

executeCommand("aCommand", console, new ResponseEvent() {

@Override
public void onSuccess(JTextArea console, String response) {
printToTheConsole(console, response);
}

@Override
public void onUpdate(JTextArea console, String response) {
printToTheConsole(console, response);
}

@Override
public void onFailure(JTextArea console, String response) {
printToTheConsole(console, response);
}
});
}

private static void printToTheConsole(JTextArea console, String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
console.append(message);
console.validate();
}
});
}

private static void executeCommand(String command, JTextArea console, ResponseEvent event) {
Runnable r = new CommandLineRunnable(command, console, event);
Thread thread = new Thread(r);
thread.start();
}

@FunctionalInterface
private interface ResponseEvent {
default void onSuccess(JTextArea console, String response) {

}

default void onUpdate(JTextArea console, String response) {

}

default void onFailure(JTextArea console, String response) {

}

}

public static class CommandLineRunnable implements Runnable {

private final String command;
private final ResponseEvent event;
private final JTextArea console;

public CommandLineRunnable(String command, JTextArea console, ResponseEvent event) {
this.command = command;
this.console = console;
this.event = event;
}

public ResponseEvent getEvent() {
return event;
}

@Override
public void run() {
Process process;
BufferedReader reader = null;
try {
process = Runtime.getRuntime().exec(getCommand());
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;

while ((line = reader.readLine()) != null) {
getEvent().onUpdate(getConsole(), line + "\n");
}

getEvent().onSuccess(getConsole(), "\n--------------------------Success!--------------------------\n");
} catch (IOException e) {
getEvent().onFailure(getConsole(), "\n--------------------------Failure!--------------------------\n");
}
}

private JTextArea getConsole() {
return console;
}

private String getCommand() {
return command;
}
}

执行后(可能在任何时间),都会执行 Runnable#run() 函数。

代码将运行,并且 ResponseEvent#onSuccess 方法或 ResponseEvent#onFailure 方法将运行被调用

然后您可以根据需要处理响应,也许可以通过更新您的 JTextArea 之一

关于java - 如果线程尚未完成,则暂停进一步执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55256864/

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