Advertisement
Guest User

Untitled

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