O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Spawn a new process from the Java code, which runs the sleep command for say 1 minute, that is, 60 seconds:
        ProcessBuilder pBuilder = new ProcessBuilder("sleep", "60");
        Process p = pBuilder.inheritIO().start();
  1. Wait for, say, 10 seconds:
        p.waitFor(10, TimeUnit.SECONDS);
  1. Check whether the process is alive:
        boolean isAlive = p.isAlive();
        System.out.println("Process alive? " + isAlive);
  1. Check whether the process can be stopped normally:
        boolean normalTermination = p.supportsNormalTermination();
        System.out.println("Normal Termination? " + normalTermination);
  1. Stop the process and check for its liveliness:
        p.destroy();
        isAlive = p.isAlive();
        System.out.println("Process ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required