fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. final ActionInterrupt interromper = new ActionInterrupt();
  14. final ActionFlag flag = new ActionFlag();
  15. flag.start();
  16. interromper.start();
  17.  
  18. try {
  19. Thread.sleep(333L);
  20. interromper.interrupt();
  21. flag.setStop();
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27.  
  28.  
  29. static class ActionInterrupt extends Thread{
  30. @Override
  31. public void run() {
  32. int pt = 0;
  33. try {
  34. while(true){
  35. System.out.println("ActionInterrupt {"+pt+"}");
  36. Thread.sleep(10L);
  37. pt++;
  38. }
  39. } catch (InterruptedException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. static class ActionFlag extends Thread{
  45. private volatile boolean running = true;
  46.  
  47. public void setStop() {
  48. running = false;
  49. }
  50.  
  51. @Override
  52. public void run() {
  53. int pt = 0;
  54. while(running){
  55. try {
  56. Thread.sleep(10L);
  57. } catch (InterruptedException e) {
  58. e.printStackTrace();
  59. }
  60. System.out.println("ActionFlag["+pt+"]");
  61. pt++;
  62. }
  63. }
  64. }
  65.  
  66. }
Success #stdin #stdout #stderr 0.04s 711168KB
stdin
Standard input is empty
stdout
ActionInterrupt {0}
ActionFlag[0]
ActionInterrupt {1}
ActionFlag[1]
ActionInterrupt {2}
ActionFlag[2]
ActionInterrupt {3}
ActionInterrupt {4}
ActionFlag[3]
ActionFlag[4]
ActionInterrupt {5}
ActionInterrupt {6}
ActionFlag[5]
ActionFlag[6]
ActionInterrupt {7}
ActionFlag[7]
ActionInterrupt {8}
ActionInterrupt {9}
ActionFlag[8]
ActionInterrupt {10}
ActionFlag[9]
ActionFlag[10]
ActionInterrupt {11}
ActionInterrupt {12}
ActionFlag[11]
ActionFlag[12]
ActionInterrupt {13}
ActionInterrupt {14}
ActionFlag[13]
ActionFlag[14]
ActionInterrupt {15}
ActionInterrupt {16}
ActionFlag[15]
ActionInterrupt {17}
ActionFlag[16]
ActionInterrupt {18}
ActionFlag[17]
ActionFlag[18]
ActionInterrupt {19}
ActionFlag[19]
ActionInterrupt {20}
ActionInterrupt {21}
ActionFlag[20]
ActionInterrupt {22}
ActionFlag[21]
ActionFlag[22]
ActionInterrupt {23}
ActionFlag[23]
ActionInterrupt {24}
ActionFlag[24]
ActionInterrupt {25}
ActionInterrupt {26}
ActionFlag[25]
ActionFlag[26]
ActionInterrupt {27}
ActionInterrupt {28}
ActionFlag[27]
ActionFlag[28]
ActionInterrupt {29}
ActionInterrupt {30}
ActionFlag[29]
ActionFlag[30]
ActionInterrupt {31}
ActionInterrupt {32}
ActionFlag[31]
ActionFlag[32]
stderr
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at Ideone$ActionInterrupt.run(Main.java:36)