我尝试使用 java 启动多个链接,但 firefox
出现错误
Firefox is already running please close first
因此,为了避免这种情况,我在启动链接之间添加了延迟。但这种延迟阻碍了我的主程序。我将这段代码作为线程,以便主程序不会阻塞,但是退出主程序会导致该线程终止而不 hibernate 。这是我的代码
main{
runCommand run= new runCommand();
run.start();
}
private class runCommand extends Thread{
@Override
public void run() {
LaunchProcess("xdg-open https://www.google.com")
Thread.sleep(8000);
LaunchProcess("xdg-open https:www.gmail.com")
}
LaunchProcess
是一个使用 runtime.getExec
执行命令的函数。上面的代码仅启动第一个链接并随着主程序退出而退出。如何确保退出主程序不会终止其启动的线程。我不想在主程序中添加 sleep
最佳答案
你想要的是join()
The join method allows one thread to wait for the completion of another.
了解有关 join()
的更多信息 here .
关于java - 主进程终止导致其线程终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125080/