【发布时间】:2017-04-28 19:49:06
【问题描述】:
我目前正在构建一个小型摇摆应用程序来格式化驱动器并更改权限并执行一些其他小事情
目前,我遇到了一个问题,即运行多个进程会导致它们异步运行,这太棒了,因为它允许我快速调度大量进程,但对于我正在做的事情,我需要该进程等待它完成之前的一个。
我遇到的问题是 process.waitFor() 方法会延迟 GUI 能够做任何事情(摆动),直到所有进程都完成。
我目前正在使用以下代码结构(我已从 this 答案中实现)来部署我的命令/进程。
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
class RunSynchronously{
private static final String sudoPassword = "1234";
public static void main(String[] args) {
Process process = null;
String[] firstCmd = {"/bin/bash", "-c", "echo " + sudoPassword + "| sudo -S chmod 777 -R /media/myuser/mydrive"};
try {
process = Runtime.getRuntime().exec(firstCmd);
} catch (IOException ex) {
Logger.getLogger(Wizard_Home.class.getName()).log(Level.SEVERE, null, ex);
}
try {
process.waitFor();
} catch (InterruptedException ex) {
Logger.getLogger(Wizard_Format.class.getName()).log(Level.SEVERE, null, ex);
}
String[] secondCmd = {"/bin/bash", "-c", "echo " + sudoPassword + "| sudo -S chmod 755 -R /media/myuser/mydrive"};
try {
process = Runtime.getRuntime().exec(secondCmd);
} catch (IOException ex) {
Logger.getLogger(Wizard_Home.class.getName()).log(Level.SEVERE, null, ex);
}
try {
process.waitFor();
} catch (InterruptedException ex) {
Logger.getLogger(Wizard_Format.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
如何在保持 GUI 处于活动状态而不是休眠/等待状态的同时延迟进程或创建队列?
【问题讨论】:
标签: java multithreading swing process synchronous