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. We will create 10 instances of ProcessBuilder to run our shell script (available at chp8/11_managing_sub_process/script.sh). We are not concerned with its output, so let's discard the output of the commands by redirecting the output to a predefined redirect called ProcessHandle.Redirect.DISCARD:
        for ( int i = 0; i < 10; i++){
          new ProcessBuilder("/bin/bash", "script.sh")
              .redirectOutput(ProcessBuilder.Redirect.DISCARD)
              .start();
        }
  1. Get the handle for the current process:
        ProcessHandle currentProcess = ProcessHandle.current();
  1. Use the current process to get its children using the children() API and iterate over each of its children to print their information. Once we have an instance of ProcessHandle, we can do multiple ...

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