首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何同步java代码

如何同步java代码
EN

Stack Overflow用户
提问于 2010-04-01 17:37:58
回答 2查看 4.4K关注 0票数 4

我有下面的代码:

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec(args);

我希望我的程序等待Runtime.getRuntime().exec(args);结束,因为它会持续2-3秒,然后继续。

想法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-04-01 17:42:17

下面是一个示例代码:

代码语言:javascript
复制
Process proc = Runtime.getRuntime().exec(ANonJava.exe@);
InputStream in = proc.getInputStream();
byte buff[] = new byte[1024];
int cbRead;

try {
    while ((cbRead = in.read(buff)) != -1) {
        // Use the output of the process...
    }
} catch (IOException e) {
    // Insert code to handle exceptions that occur
    // when reading the process output
}

// No more output was available from the process, so...

// Ensure that the process completes
try {
    proc.waitFor();
} catch (InterruptedException) {
    // Handle exception that could occur when waiting
    // for a spawned process to terminate
}

// Then examine the process exit code
if (proc.exitValue() == 1) {
    // Use the exit value...
}

你可以在这个网站上找到更多信息:http://docs.rinet.ru/JWP/ch14.htm

票数 2
EN

Stack Overflow用户

发布于 2010-04-01 17:40:47

使用Process.waitFor()

代码语言:javascript
复制
Process p = Runtime.getRuntime().exec(args);
int status = p.waitFor();

来自JavaDoc:

如果有必要,

会使当前线程等待,直到该process对象所代表的进程终止。如果子进程已经终止,此方法将立即返回。如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2559430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档