Guest User

Untitled

a guest
Jan 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package threads;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. public class MyRunnable implements Runnable {
  5.  
  6. private int var;
  7. public volatile boolean shutdown;
  8.  
  9.  
  10. public MyRunnable(int var) {
  11. this.var = var;
  12. }
  13.  
  14. @Override
  15. public void run() {
  16.  
  17. try {
  18. while (!Thread.currentThread().isInterrupted()) {
  19. String cmd = "gedit";
  20.  
  21. Process proc = Runtime.getRuntime().exec(cmd);
  22.  
  23. System.out.println("Command executed");
  24. String line;
  25. try (BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
  26. while ((line = input.readLine()) != null) {
  27. System.out.println(line);
  28. }
  29. }
  30.  
  31. BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
  32.  
  33. String s = null;
  34.  
  35. // read any errors from the attempted command
  36. System.out.println("Here is the standard error of the command (if any):n");
  37. while ((s = stdError.readLine()) != null) {
  38. System.out.println(s);
  39. }
  40. }
  41.  
  42. } catch (Exception e) {
  43. System.err.println("gedit error : " + e.getLocalizedMessage());
  44. }
  45. }
  46.  
  47. }
  48.  
  49. package threads;
  50. public class Threads {
  51.  
  52. /**
  53. * @param args the command line arguments
  54. * @throws java.lang.InterruptedException
  55. */
  56. public static void main(String[] args) throws InterruptedException {
  57. MyRunnable myRunnable = new MyRunnable(0);
  58. Thread t = new Thread(myRunnable);
  59. System.out.println("t.getState 1 : " + t.getState());
  60. t.start();
  61. Thread.sleep(2000);
  62. t.interrupt();
  63. System.out.println("t.getState 2 : " + t.getState());
  64. }
  65. }
  66.  
  67. public class Executor {
  68.  
  69. private Process process;
  70. private final String command;
  71.  
  72. Executor(String command) {
  73. this.command = command;
  74. }
  75.  
  76. public void run() throws Exception {
  77.  
  78. process = Runtime.getRuntime().exec(command);
  79. System.out.println("Command executed");
  80.  
  81. Thread runner = new Thread(() -> {
  82. try {
  83. try (BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
  84. String line;
  85. while (process.isAlive() && (line = input.readLine()) != null)
  86. System.out.println(line);
  87. }
  88.  
  89.  
  90. //небольшая задержка, т.к. иногда не успевает проставиться флаг
  91. TimeUnit.MILLISECONDS.sleep(100);
  92. if (!process.isAlive()) {
  93. System.out.println("process " + command + " has been stopped!");
  94. return;
  95. }
  96.  
  97. // read any errors from the attempted command
  98. System.out.println("Here is the standard error of the command (if any):n");
  99. try (BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
  100. String errorCode;
  101. while (process.isAlive() && (errorCode = stdError.readLine()) != null)
  102. System.out.println(errorCode);
  103. }
  104.  
  105. } catch (Exception e) {
  106. System.err.println(command + " error : " + e.getLocalizedMessage());
  107. }
  108. });
  109.  
  110. runner.start();
  111. }
  112.  
  113. public void shutdown() {
  114. process.destroy();
  115. }
  116. }
  117.  
  118. Executor process = new Executor("pluma");
  119. //запуск
  120. process.run();
  121. //остановка
  122. process.shutdown();
Add Comment
Please, Sign In to add comment