Sameera.San
Sameera.San

Reputation: 77

Java process waitFor() function causes application to freeze

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.

Upvotes: 4

Views: 1710

Answers (2)

Rahul Rabhadiya
Rahul Rabhadiya

Reputation: 430

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.

Upvotes: 2

strash
strash

Reputation: 1321

Yes you should do that work in a separate thread. Read about Runnable interface and Thread class

Upvotes: 1

Related Questions