Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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());
- }
- }
- }
- 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());
- }
- }
- public class Executor {
- private Process process;
- private final String command;
- Executor(String command) {
- this.command = command;
- }
- public void run() throws Exception {
- process = Runtime.getRuntime().exec(command);
- System.out.println("Command executed");
- Thread runner = new Thread(() -> {
- try {
- try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
- String line;
- while (process.isAlive() && (line = input.readLine()) != null)
- System.out.println(line);
- }
- //небольшая задержка, т.к. иногда не успевает проставиться флаг
- TimeUnit.MILLISECONDS.sleep(100);
- if (!process.isAlive()) {
- System.out.println("process " + command + " has been stopped!");
- return;
- }
- // read any errors from the attempted command
- System.out.println("Here is the standard error of the command (if any):n");
- try (BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
- String errorCode;
- while (process.isAlive() && (errorCode = stdError.readLine()) != null)
- System.out.println(errorCode);
- }
- } catch (Exception e) {
- System.err.println(command + " error : " + e.getLocalizedMessage());
- }
- });
- runner.start();
- }
- public void shutdown() {
- process.destroy();
- }
- }
- Executor process = new Executor("pluma");
- //запуск
- process.run();
- //остановка
- process.shutdown();
Add Comment
Please, Sign In to add comment