Java wait wait () function causes application to hang

In 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 that it takes a while to back up the logs and get a response, until then my application hangs. If I remove the function call pr.waitFor()

, I get a response, but the log backup doesn't work.

+3


source to share


2 answers


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



So, you can create another thread to execute the command. pr = rt.exec (command) ;, you may need to perform this task asynchronously. Because until the subprocess completion process completes, it will wait.

+2


source


Yes, you have to do this work in a separate thread. Read about the Runnable interface and the Thread class



+1


source







All Articles