Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java process waitFor() function causes application to freeze

Tags:

java

wait

exec

Within my java application, I have a log backup function:

rt = Runtime.getRuntime();
pr = rt.exec(command);
int exitVal = pr.waitFor();

if(exitVal == 0) 
   return true

The problem is it takes a while to backup the logs and to get a response, until then my application freezes. If I remove the pr.waitFor() function call, I get a response, but the log backup fails to work.

like image 556
Sameera.San Avatar asked Sep 13 '25 10:09

Sameera.San


1 Answers

waitFor() method causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

So you can create another thread which do execution of command. pr = rt.exec(command); . You might have to perform this task asynchronously. Because until subprocess get terminated process will wait for.

like image 142
Rahul Rabhadiya Avatar answered Sep 14 '25 23:09

Rahul Rabhadiya