package io.vividcode.feature9.process;

import java.io.IOException;

public class LongRunningProcess {

  public ProcessHandle start() throws IOException {
    final ProcessBuilder processBuilder = new ProcessBuilder("top")
        .inheritIO();
    return processBuilder.start().toHandle();
  }

  public void waitFor(final ProcessHandle processHandle) {
    processHandle.onExit().whenCompleteAsync((handle, throwable) -> {
      if (throwable == null) {
        System.out.println(handle.pid());
      } else {
        throwable.printStackTrace();
      }
    });
    final Thread shutdownThread = new Thread(() -> {
      try {
        Thread.sleep(1000);
      } catch (final InterruptedException e) {
        e.printStackTrace();
      }
      if (processHandle.supportsNormalTermination()) {
        processHandle.destroy();
      } else {
        processHandle.destroyForcibly();
      }
    });
    shutdownThread.start();
    try {
      shutdownThread.join();
      Thread.sleep(1000);
    } catch (final InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void main(final String[] args) {
    final LongRunningProcess longRunningProcess = new LongRunningProcess();
    try {
      longRunningProcess.waitFor(longRunningProcess.start());
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
}

@@ -60,16 +60,60 @@
 ception;
+%0Aimport java.util.concurrent.CountDownLatch;
 %0A%0Apublic
@@ -141,18 +141,17 @@
 cess %7B%0A%0A
-  
+%09
 public P
@@ -192,20 +192,18 @@
 ption %7B%0A
-    
+%09%09
 final Pr
@@ -258,24 +258,20 @@
 (%22top%22)%0A
-        
+%09%09%09%09
 .inherit
@@ -276,20 +276,18 @@
 itIO();%0A
-    
+%09%09
 return p
@@ -320,23 +320,21 @@
 ndle();%0A
-  %7D%0A%0A  
+%09%7D%0A%0A%09
 public v
@@ -382,20 +382,72 @@
 ndle) %7B%0A
-    
+%09%09final CountDownLatch latch = new CountDownLatch(1);%0A%09%09
 processH
@@ -504,22 +504,19 @@
 e) -%3E %7B%0A
-      
+%09%09%09
 if (thro
@@ -532,24 +532,20 @@
 null) %7B%0A
-        
+%09%09%09%09
 System.o
@@ -570,22 +570,19 @@
 pid());%0A
-      
+%09%09%09
 %7D else %7B
@@ -582,24 +582,20 @@
  else %7B%0A
-        
+%09%09%09%09
 throwabl
@@ -615,36 +615,51 @@
 race();%0A
-      %7D%0A    %7D);%0A    
+%09%09%09%7D%0A%09%09%09latch.countDown();%0A%09%09%7D);%0A%09%09
 final Th
@@ -703,28 +703,21 @@
 %3E %7B%0A
-      try %7B%0A        
+%09%09%09try %7B%0A%09%09%09%09
 Thre
@@ -732,22 +732,19 @@
 (1000);%0A
-      
+%09%09%09
 %7D catch 
@@ -776,24 +776,20 @@
 on e) %7B%0A
-        
+%09%09%09%09
 e.printS
@@ -805,22 +805,16 @@
 ();%0A
-      %7D%0A      
+%09%09%09%7D%0A%09%09%09
 if (
@@ -858,24 +858,20 @@
 on()) %7B%0A
-        
+%09%09%09%09
 processH
@@ -887,22 +887,19 @@
 troy();%0A
-      
+%09%09%09
 %7D else %7B
@@ -903,16 +903,12 @@
 e %7B%0A
-        
+%09%09%09%09
 proc
@@ -940,28 +940,21 @@
 ();%0A
-      %7D%0A    %7D);%0A    
+%09%09%09%7D%0A%09%09%7D);%0A%09%09
 shut
@@ -973,32 +973,27 @@
 tart();%0A
-    
+%09%09
 try %7B%0A
-      
+%09%09%09
 shutdown
@@ -1011,38 +1011,28 @@
 ();%0A
-      Thread.sleep(1000);%0A    
+%09%09%09latch.await();%0A%09%09
 %7D ca
@@ -1060,38 +1060,35 @@
 dException e) %7B%0A
-      
+%09%09%09
 e.printStackTrac
@@ -1096,21 +1096,17 @@
 ();%0A
-    %7D%0A  %7D%0A%0A  
+%09%09%7D%0A%09%7D%0A%0A%09
 publ
@@ -1148,20 +1148,18 @@
 args) %7B%0A
-    
+%09%09
 final Lo
@@ -1226,24 +1226,19 @@
 ();%0A
-    
+%09%09
 try %7B%0A
-      
+%09%09%09
 long
@@ -1289,20 +1289,18 @@
 art());%0A
-    
+%09%09
 %7D catch 
@@ -1327,14 +1327,11 @@
 ) %7B%0A
-      
+%09%09%09
 e.pr
@@ -1351,16 +1351,13 @@
 ();%0A
-    %7D%0A  
+%09%09%7D%0A%09
 %7D%0A%7D%0A

Update ProcessHandle example