Wednesday, 17 September 2014

CyclicBarrier

CyclicBarrier is a synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. Cyclic barrier is a barrier, that all thread must wait at that point, until all the threads reach this point.

CyclicBarrier class provides two constructors.

1. CyclicBarrier(int parties)
Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and does not perform a predefined action when the barrier is tripped.

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Producer implements Runnable{
    CyclicBarrier barrier = null;
    int iter = 0;
    
    Producer(CyclicBarrier barrier){
        this.barrier = barrier;
    }
    
    @Override
    public void run(){
        while(true){
            try {
                barrier.await();
                iter++;
            } catch (InterruptedException | BrokenBarrierException ex) {
                Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println(Thread.currentThread().getName() +": finished " + iter + " times"); 
        }
    }
}

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierEx {
    public static void main(String args[]){
        CyclicBarrier barrier = new CyclicBarrier(5);
        
        Producer p1 = new Producer(barrier);
        Producer p2 = new Producer(barrier);
        Producer p3 = new Producer(barrier);
        Producer p4 = new Producer(barrier);
        Producer p5 = new Producer(barrier);
        
        Thread t1 = new Thread(p1);
        Thread t2 = new Thread(p2);
        Thread t3 = new Thread(p3);
        Thread t4 = new Thread(p4);
        Thread t5 = new Thread(p5);
        
        t1.setName("Producer1");
        t2.setName("Producer2");
        t3.setName("Producer3");
        t4.setName("Producer4");
        t5.setName("Producer5");
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();        
    }
}

Output
Producer2: finished 1 times
Producer3: finished 1 times
Producer1: finished 1 times
Producer5: finished 1 times
Producer4: finished 1 times
Producer4: finished 2 times
Producer1: finished 2 times
Producer5: finished 2 times
Producer3: finished 2 times
Producer2: finished 2 times
Producer2: finished 3 times
Producer5: finished 3 times
Producer1: finished 3 times
Producer4: finished 3 times
Producer3: finished 3 times
Producer3: finished 4 times
Producer1: finished 4 times
Producer5: finished 4 times
Producer2: finished 4 times
Producer4: finished 4 times
Producer4: finished 5 times
Producer1: finished 5 times
Producer5: finished 5 times
Producer3: finished 5 times
Producer2: finished 5 times
Producer2: finished 6 times
Producer3: finished 6 times
Producer5: finished 6 times




2. CyclicBarrier(int parties, Runnable barrierAction)
Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and which will execute the given barrier action when the barrier is tripped, performed by the last thread entering the barrier.

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierEx {
    public static void main(String args[]){
        CyclicBarrier barrier = new CyclicBarrier(5, new Runnable(){
            public void run(){
                System.out.println("------------------------------");
            }
        });
        
        Producer p1 = new Producer(barrier);
        Producer p2 = new Producer(barrier);
        Producer p3 = new Producer(barrier);
        Producer p4 = new Producer(barrier);
        Producer p5 = new Producer(barrier);
        
        Thread t1 = new Thread(p1);
        Thread t2 = new Thread(p2);
        Thread t3 = new Thread(p3);
        Thread t4 = new Thread(p4);
        Thread t5 = new Thread(p5);
        
        t1.setName("Producer1");
        t2.setName("Producer2");
        t3.setName("Producer3");
        t4.setName("Producer4");
        t5.setName("Producer5");
        
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();        
    }
}

Output
------------------------------
Producer5: finished 1 times
Producer3: finished 1 times
Producer2: finished 1 times
Producer1: finished 1 times
Producer4: finished 1 times
------------------------------
Producer4: finished 2 times
Producer3: finished 2 times
Producer5: finished 2 times
Producer1: finished 2 times
Producer2: finished 2 times
------------------------------
Producer2: finished 3 times
Producer5: finished 3 times
Producer3: finished 3 times
Producer4: finished 3 times
Producer1: finished 3 times
------------------------------
Producer1: finished 4 times




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment