Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. public abstract class ParallelCappedRunnable implements Runnable {
  2.  
  3. private final Lock lock;
  4. private final Condition condition;
  5. private final ParallelCappedMultithreader pcm;
  6.  
  7. public ParallelCappedRunnable(ParallelCappedMultithreader pcm, Lock lock, Condition condition) {
  8. this.lock = lock;
  9. this.condition = condition;
  10. this.pcm = pcm;
  11. }
  12.  
  13. @Override
  14. public final void run() {
  15. doTask();
  16. pcm.sendSignal();
  17. if(pcm.getMissingSignals() == 0) {
  18. lock.lock();
  19. try {
  20. condition.signal();
  21. } finally {
  22. lock.unlock();
  23. }
  24. }
  25. }
  26.  
  27. public abstract void doTask();
  28. }
  29.  
  30. public class ParallelCappedMultithreader {
  31.  
  32. private AtomicInteger missingSignals = new AtomicInteger (0);
  33. private final int MAX_THREADS;
  34. private final Lock lock = new ReentrantLock();
  35. private final Condition condition = lock.newCondition();
  36. private final Class runnable, parentClass;
  37. private final Object parentInstance;
  38.  
  39. public ParallelCappedMultithreader(Class<? extends ParallelCappedRunnable> runnable, int threads, Class parentClass, Object parentInstance) {
  40. this.runnable = runnable;
  41. this.parentClass = parentClass;
  42. this.MAX_THREADS = threads;
  43. this.parentInstance = parentInstance;
  44. }
  45. public ParallelCappedMultithreader(Class<? extends ParallelCappedRunnable> runnable, int threads) {
  46. this(runnable, threads, null, null);
  47. }
  48.  
  49.  
  50. public void start() throws InterruptedException{
  51. try {
  52. missingSignals.set(MAX_THREADS);
  53. Constructor constructor;
  54. if(parentClass == null) {
  55. constructor = runnable.getDeclaredConstructor(ParallelCappedMultithreader.class, Lock.class, Condition.class);
  56. }
  57. else {
  58. constructor = runnable.getDeclaredConstructor(parentClass, ParallelCappedMultithreader.class, Lock.class, Condition.class);
  59. }
  60. for(int i = 0; i < MAX_THREADS; i++) {
  61. if(parentClass == null)
  62. (new Thread((Runnable)constructor.newInstance(this, lock, condition))).start();
  63. else
  64. (new Thread((Runnable)constructor.newInstance(parentInstance, this, lock, condition))).start();
  65. }
  66. } catch (Exception ex) {
  67. Logger.getLogger(ParallelCappedMultithreader.class.getName()).log(Level.SEVERE, null, ex);
  68. System.exit(0);
  69. }
  70. lock.lock();
  71. try {
  72. condition.await();
  73. } finally {
  74. lock.unlock();
  75. }
  76. }
  77.  
  78. public int getMissingSignals() {
  79. return missingSignals.get();
  80. }
  81.  
  82. public void sendSignal() {
  83. System.out.println(missingSignals.getAndDecrement());
  84.  
  85. }
  86. }
  87.  
  88. MyRunnable(List threads) {
  89. lock (threads) {
  90. threads.add(this);
  91. }
  92. }
  93.  
  94. void run() {
  95. doStuff()
  96.  
  97. lock (threads) {
  98. threads.remove(this);
  99.  
  100. if (threads.size() == 0)
  101. threads.notify();
  102. }
  103. }
  104.  
  105. void startAndWait(int numThreads) {
  106. List<MyRunnable> threads = new ArrayList<MyRunnable>();
  107.  
  108. for (int i=0; i<numThreads; i++)
  109. new Thread(new MyRunnable(threads)).start();
  110.  
  111. lock (threads) {
  112. while (threads.size() > 0)
  113. wait();
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement