There is a thread that starts a process. Then you need to complete the stream along with the process. How to do it?
Stream class code:
package threads; import java.io.BufferedReader; import java.io.InputStreamReader; public class MyRunnable implements Runnable { private int var; public volatile boolean shutdown; public MyRunnable(int var) { this.var = var; } @Override public void run() { try { while (!Thread.currentThread().isInterrupted()) { String cmd = "gedit"; Process proc = Runtime.getRuntime().exec(cmd); System.out.println("Command executed"); String line; try (BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()))) { while ((line = input.readLine()) != null) { System.out.println(line); } } BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); String s = null; // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } } } catch (Exception e) { System.err.println("gedit error : " + e.getLocalizedMessage()); } } } Actually the main method:
package threads; public class Threads { /** * @param args the command line arguments * @throws java.lang.InterruptedException */ public static void main(String[] args) throws InterruptedException { MyRunnable myRunnable = new MyRunnable(0); Thread t = new Thread(myRunnable); System.out.println("t.getState 1 : " + t.getState()); t.start(); Thread.sleep(2000); t.interrupt(); System.out.println("t.getState 2 : " + t.getState()); } } Output:
run: t.getState 1: NEW
Command executed
t.getState 2: RUNNABLE
Well, gedit itself is also safely executed.
What is the correct method to stop the stream with the gedit process?