How to sync java code

I have the following code:

Process p = Runtime.getRuntime().exec(args);

and I want my program to wait for Runtime.getRuntime (). exec (args); to finish work in the last 2-3 seconds, and then continue.

Ideas?

+3
source share
2 answers

Here is a sample code:

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...
}

You can find more on this site: http://docs.rinet.ru/JWP/ch14.htm

+2
source

use Process.waitFor () :

Process p = Runtime.getRuntime().exec(args);
int status = p.waitFor();

From JavaDoc:

, , , , , . , . , .

+5

Source: https://habr.com/ru/post/1739442/


All Articles