导致主进程等待另一个进程完成
我在java中遇到同步问题。 我希望我的主线程等待进程“p1”完成。 我使用了“等待”方法。它对我不起作用。
Process p1 = runtime.exec("cmd /c start /MIN " + path + "aBatchFile.bat" );
p1.waitFor();
有人可以帮我吗?
太感谢了。
I have a synchronization problem in java.
I want my main thread to wait until process "p1" is finished.
I have used "waitfor" method. it has not worked for me.
Process p1 = runtime.exec("cmd /c start /MIN " + path + "aBatchFile.bat" );
p1.waitFor();
Could anybody help me please?
Thank you so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是,您从
exec()
返回的Process
对象代表您启动的cmd.exe
实例。您的cmd.exe
实例执行一件事:它启动一个批处理文件,然后退出(无需等待批处理文件,因为这就是start代码>命令执行)。此时,您的
waitFor()
返回。为了避免此问题,您应该能够直接运行批处理文件:
或者,尝试
/wait
命令行选项:The problem here is that the
Process
object you get back fromexec()
represents the instance ofcmd.exe
that you start. Your instance ofcmd.exe
does one thing: it starts a batch file and then exits (without waiting for the batch file, because that's what thestart
command does). At that point, yourwaitFor()
returns.To avoid this problem, you should be able to run the batch file directly:
Alternately, try the
/wait
command line option: