Advertisement
Jaydeep999997

Barrier Implementation

Oct 22nd, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | Source Code | 0 0
  1. class Demonstration {
  2.   public static void main(String[] args) throws Exception {
  3.     CyclicBarrier.runTest();
  4.   }
  5. }
  6.  
  7. class CyclicBarrier {
  8.   public static void runTest() throws InterruptedException {
  9.     CyclicBarrier barrier = new CyclicBarrier(3);
  10.  
  11.     Thread t1 =
  12.         new Thread(
  13.             new Runnable() {
  14.  
  15.               @Override
  16.               public void run() {
  17.                 try {
  18.                   System.out.println("T1");
  19.                   barrier.await();
  20.  
  21.                   // Thread 1 will be blocked on the barrier so won't be reaching here until
  22.                   // previous barrier is cleared.
  23.                   System.out.println("T1");
  24.                   barrier.await();
  25.                   System.out.println("T1");
  26.                   barrier.await();
  27.                 } catch (InterruptedException ie) {
  28.  
  29.                 }
  30.               }
  31.             });
  32.  
  33.     Thread t2 =
  34.         new Thread(
  35.             () -> {
  36.               try {
  37.                 Thread.sleep(500);
  38.                 System.out.println("T2");
  39.                 barrier.await();
  40.                 Thread.sleep(500);
  41.                 System.out.println("T2");
  42.                 barrier.await();
  43.                 Thread.sleep(500);
  44.                 System.out.println("T2");
  45.                 barrier.await();
  46.               } catch (InterruptedException ie) {
  47.  
  48.               }
  49.             });
  50.  
  51.     Thread t3 =
  52.         new Thread(
  53.             () -> {
  54.               try {
  55.                 Thread.sleep(1000);
  56.                 System.out.println("T3");
  57.                 barrier.await();
  58.                 Thread.sleep(1000);
  59.                 System.out.println("T3");
  60.                 barrier.await();
  61.                 Thread.sleep(1000);
  62.                 System.out.println("T3");
  63.                 barrier.await();
  64.               } catch (InterruptedException ie) {
  65.  
  66.               }
  67.             });
  68.  
  69.     t1.start();
  70.     t2.start();
  71.     t3.start();
  72.  
  73.     t1.join();
  74.     t2.join();
  75.     t3.join();
  76.   }
  77.  
  78.   private int threadsToWaitFor;
  79.   private int threadsWaiting = 0;
  80.   private int threadsToRelease = 0;
  81.  
  82.   public CyclicBarrier(int threadsToWaitFor) {
  83.     this.threadsToWaitFor = threadsToWaitFor;
  84.   }
  85.  
  86.   public synchronized void await() throws InterruptedException {
  87.     // No new thread is allowed for a new Barrier until we have reset the current barrier.
  88.     while (threadsWaiting == threadsToWaitFor) {
  89.       wait();
  90.     }
  91.  
  92.     threadsWaiting++;
  93.  
  94.     // All threads have reached the barrier.
  95.     if (threadsWaiting == threadsToWaitFor) {
  96.       threadsToRelease = threadsToWaitFor;
  97.  
  98.       // Notify remaining threads who have already reached the barrier.
  99.       notifyAll();
  100.     } else {
  101.       // Wait for all the threads to arrive at the barrier.
  102.       while (threadsWaiting < threadsToWaitFor) {
  103.         wait();
  104.       }
  105.     }
  106.  
  107.     // We freed up the current thread.
  108.     threadsToRelease--;
  109.  
  110.     // If this is the last thread to be freed up, we can reset the barrier.
  111.     if (threadsToRelease == 0) {
  112.       threadsWaiting = 0;
  113.  
  114.       // New threads might have attempted for a new barrier, we want to notify them.
  115.       notifyAll();
  116.     }
  117.   }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement