Guest User

Untitled

a guest
Jan 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. First(new Thread() {
  2. public void run() {
  3. //do something
  4.  
  5. }
  6. });
  7.  
  8. public class FirstCaller {
  9.  
  10. private void method1() { }
  11. private void method2() { }
  12. private void method3() { }
  13.  
  14. public void someMethod() {
  15. First(new Thread() {
  16. public void run() {
  17. //do something
  18. method1();
  19. method2();
  20. method3();
  21. }
  22. });
  23. }
  24. }
  25.  
  26. public class FirstCaller {
  27. public void someMethod() {
  28. new First(new Thread() {
  29. private void method1() { }
  30. private void method2() { }
  31. private void method3() { }
  32.  
  33. public void run() {
  34. //do something
  35. method1();
  36. method2();
  37. method3();
  38. }
  39. });
  40. }
  41. }
  42.  
  43. ExecutorService service = Executors.newSingleThreadedPool();
  44.  
  45. service.submit(runnable1);
  46. service.submit(runnable2);
  47. service.submit(runnable3);
  48.  
  49. public class MultiRunnable implements Runnable {
  50.  
  51. private Runnable runnable1;
  52. private Runnable runnable2;
  53.  
  54.  
  55. public MultiRunnable(Runnable runnable1, Runnable runnable2) {
  56. this.runnable1 = runnable1;
  57. this.runnable2 = runnable2;
  58. }
  59.  
  60. @Override
  61. public void run() {
  62. runnable1.run();
  63. runnable2.run();
  64. }
  65. }
  66.  
  67. import java.util.Arrays;
  68. import java.util.List;
  69.  
  70. public class MultiRunnable implements Runnable {
  71.  
  72. private List<Runnable> runnables;
  73.  
  74. public MultiRunnable(Runnable... runnables) {
  75. this.runnables = Arrays.asList(runnables);
  76. }
  77. public MultiRunnable(List<Runnable> runnables) {
  78. this.runnables = runnables;
  79. }
  80.  
  81. @Override
  82. public void run() {
  83. for(Runnable runnable : runnables)
  84. runnable.run();
  85. }
  86. }
  87.  
  88. class MyThread extends Thread {
  89. public enum Action { A, B, C }
  90. private Action mAction;
  91. public void run() {
  92. if (mAction == null) {
  93. throw new IllegalStateException("Action must be specified");
  94. }
  95. switch (mAction) {
  96. case A:
  97. methodA();
  98. break;
  99. case B:
  100. methodB();
  101. break;
  102. case C:
  103. methodC();
  104. break;
  105. }
  106. }
  107. public void setAction(Action action) {
  108. if (action == null) {
  109. throw new IllegalArgumentException("Action cannot be null");
  110. }
  111. mAction = action;
  112. }
  113. private void methodA() { ... }
  114. private void methodB() { ... }
  115. private void methodC() { ... }
  116. }
  117.  
  118. private EecutorService threadPool = Executors.newSingleThreadedPool();
  119. ...
  120. threadPool.submit(new First());
  121. threadPool.submit(new Second());
  122. threadPool.submit(new Third());
  123. ...
  124. // when you are done submitting, always shutdown your pool
  125. threadPool.shutdown();
Add Comment
Please, Sign In to add comment