Java Concurrency(pseudo -parallelism): Part I

  • ProcessBuilder class is introduced in JDK 5.0, is used to create processes.
  • This class is not synchronised
  • Package :- java.lang.ProcessBuilder
EX:
// Java code illustrating command() method
import java.lang.*;
import java.io.*;
class ProcessBuilderDemo{
public static void main(String[] arg) throws IOException{
// creating list of process
List<String> list = new ArrayList<String>();
list.add("notepad.exe");
// create the process
ProcessBuilder build = new ProcessBuilder(list);
// checking the command i list
System.out.println("command: " + build.command());
}
}
O/p - command: [notepad.exe]
Process process = new ProcessBuilder("java", "- version").start(); 
ProcessBuilder processBuilder = new ProcessBuilder();
Map<String, String> environment = processBuilder.environment();
environment.forEach((key, value) -> System.out.println(key + value));
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "echo Hello %name%");
Map<String, String> environment = processBuilder.environment();
environment.put("name", "Shivam");
Process p = processBuilder.start();
String line;
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = r.readLine()) != null) {
System.out.println(line);
}
r.close();
O/P - Shivam
ProcessBuilder processBuilder = new ProcessBuilder("java","version");
processBuilder.redirectErrorStream(true);
File log = folder.newFile("java-version.log");
processBuilder.redirectOutput(log);
Process process = processBuilder.start();
String[] command = {"/teste/teste_back/script.sh", argument1, argument};
ProcessBuilder p = new ProcessBuilder(command);
try {
// create a process builder to send a command and a argument
Process p2 = p.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p2.getInputStream()));

String line;
log.info("Output of running " + command + " is: ");
System.out.println("Output of running " + command + " is: ");
while ((line = br.readLine()) != null) {
log.info(line);
}
}
  • Before JDK 5.0, Runtime.exec() is used to create the process.
Runtime r = Runtime.getRuntime();
Process p = r.exec("firefox");
p.waitFor(10, TimeUnit.SECONDS);
p.destroy();
String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
CommandLine cmdLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);

--

--

--

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Asus ZenBook Screen Pad is Not Working After Windows Update | FIX

The SQL vs NoSQL

The Allen Algorithm: GTD in a Nutshell

Design Patterns used in Swift for iOS App Development

How did Android SurfaceView Works?

Floating action button with badge counter

Static Libraries in a nutshell

Communication between two separate GUIs via Multi-Threading

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kumar Shivam

Kumar Shivam

Technical Consultant | Passionate about exploring new Technology | Cyber Security Enthusiast | Technical Blogger | Problem Solver

More from Medium

Heap & Stack in Java

Logging in Java

Environment setup for spring boot applications — Step 1, install jdk

Java Concurrency API