I have a process that creates another process. The first process waits for the second process to print "done" then continues. However, I am getting a race condition where the first process gets stuck sometimes and can't continue after the second process prints. My code is:
First Process (process is created with ProcessBuilder):
if((new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()).contains("Process done"))
{
}
System.out.println("Second process completed.");
process.destroy();
Second Process:
public static void main(String[] args)
{
/*does some action*/
System.out.println("Process done");
Thread.sleep(Integer.MAX_VALUE);
}
Sometimes this works, but other times the first process will not get the input stream and the program will hang. What is the problem?