gpt4 book ai didi

java - 在 Java 中执行进程而不卡住 JFrame

转载 作者:行者123 更新时间:2023-12-01 08:07:26 24 4
gpt4 key购买 nike

如何在 Java 中执行进程而不导致程序卡住?我尝试过使用 SwingWorker,但我还不太明白它是如何工作的。

还有其他方法可以实现此目的吗?我想在我的 JDroidLib 中使用类似的东西。如需完整源代码,请查看 GitHub:http://github.com/Team-M4gkBeatz/JDroidLib

提前致谢!

编辑:

感谢您的回答。但我有一个包含多种方法的类(嗯,它不止一个类,但你明白我的意思);我如何使用 SwingWorker 与它们交互?

这是其中一类:

/**
*
* @author Simon
*/
public abstract class Command extends SwingWorker {

BufferedReader prReader = null;
ProcessBuilder process = null;
Process pr = null;
Date timeNow = new Date();
String osName = System.getProperty("os.name");

public void executeProcessNoReturn(String _process, String arg) throws IOException {
process = new ProcessBuilder(_process, arg);
pr = process.start();
}

public String executeProcessReturnLastLine(String _process, String arg) throws IOException {
process = new ProcessBuilder(_process, arg);
pr = process.start();
prReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = prReader.readLine()) != null) {
// Wait for input to end.
}
return line;
}

public StringBuilder executeProcessReturnAllOutput(String _process, String arg) throws IOException {
process = new ProcessBuilder(_process, arg);
pr = process.start();
prReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder output = null;
String line;
while ((line = prReader.readLine()) != null) {
output.append(line);
}
return output;
}

public boolean isProcessRunning(String processName) throws IOException {
boolean value = false;
if (osName.equals("Linux") | osName.contains("Mac")) {
process = new ProcessBuilder("ps", "-e");
pr = process.start();
String line;
prReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line = prReader.readLine()) != null) {
if (line.contains(processName)) { value = true; break; }
}
} else {
String winDir = System.getenv("windir") + "/System32/tasklist.exe";
process = new ProcessBuilder(winDir);
pr = process.start();
String line;
prReader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while ((line = prReader.readLine()) != null) {
if (line.contains(processName)) { value = true; break; }
}
}

return value;

}

public String executeProcessReturnError(String processName, String arg) throws IOException {
process = new ProcessBuilder(processName, arg);
process.redirectError();
pr = process.start();
prReader = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line;
String output = "";
while ((line = prReader.readLine()) != null) {
output += line + "\n";
}
return output;
}
}

最佳答案

是的,你可以使用 SwingWorker 这个想法是,一个需要花费大量时间在单独的线程(后台线程)中运行的任务然后你不会阻塞你的 gui 并且你的 JFrame 是不会卡住。这是我非常喜欢的完整示例 Swing Worker Example .

基本上作为一个示例,您创建自己的类来扩展 SwingWorker 并覆盖 doInBackground

注意: 您可以像普通类一样拥有字段。

示例:

class Worker extends SwingWorker<Void, String> {
private SomeClass businessDelegate;
private JLabel label;

@Override
protected Void doInBackground() throws Exception {
//here you make heavy task this is running in another thread not in EDT
businessDelegate.callSomeService();
setProgress(30); // this is if you want to use with a progressBar
businessDelegate.saveToSomeDataBase();
publish("Processes where saved");
return null;
}

@Override
protected void process(List<String> chunks){
//this is executed in EDT you can update a label for example
label.setText(chunks.toString());
}

//add setters for label and businessDelegate
}

您还了解了 process(..) publish(..)done()

然后将其放入您的客户端代码中。

SwingWorker<Void,String> myWorker = new Worker();
myWorker.execute();

关于java - 在 Java 中执行进程而不卡住 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423980/

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