Tuesday, June 15, 2010

CyclicBarrier Test

import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTester {
    private static final int numOfThreads = 5;
   
    public static void main (String[] args) {
        startTasks ();
    }


    private static void startTasks () {
        try {
            CyclicBarrier barrier = buildBarrier();
            startThreads(barrier);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

 
    private static CyclicBarrier buildBarrier () {
        CyclicBarrier barrier = new CyclicBarrier (numOfThreads, new Runnable () {
            public void run () {
                Logger.log (LogLevel.LOG, " -- barrier action done");
            }
        });
        return barrier;
    }
   
    private static void startThreads (CyclicBarrier barrier) {
        Thread[] threads = new Thread[numOfThreads];
        for (int i = 0; i < threads.length; i++) {
            TaskExecuter executor = new TaskExecuter ();
            executor.setName ("CleanupThread-" + i);
            executor.setBarrier (barrier);
            threads[i] = executor;
            threads[i].start ();
        }
        for (int i = 0; i < threads.length; i++) {
            try {
                threads[i].join ();
            } catch (InterruptedException ignore) {
            }
        }
    }

}









import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.BrokenBarrierException;

public class TaskExecuter extends Thread {
    private CyclicBarrier barrier ;

    public void setBarrier (CyclicBarrier barrier){
        this.barrier = barrier;
    }

    public void run () {
            Logger.log (LogLevel.LOG, "   --- {0} is started processing....", this.getName ());
            try {
                doAction ();
            } catch (Exception e) {
                Logger.log(LogLevel.EXCEPTION,"[{0}]",e);
            }
            Logger.log (LogLevel.LOG, "   --- {0} is finished processing....", this.getName ());
    }

    private void doAction () {
            try {
                //do your work
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                waitForOthers ();
            }
    }

    private void waitForOthers () {
            try {
                Logger.log (LogLevel.LOG, "{0} is waiting for the others, currentwaiters count :{1}",
                    getName (), barrier.getNumberWaiting ());
                barrier.await (100000L, TimeUnit.MILLISECONDS);
                Logger.log (LogLevel.LOG, "{0} is out of the barrier ", getName ());
            } catch (BrokenBarrierException e) {
                barrier.reset();
                Logger.log (LogLevel.WARNING, " -- resetting the barrier for the exception : {0}", e);
            } catch (InterruptedException ex) {
                Logger.log (LogLevel.EXCEPTION, " --{0}", ex);
            } catch (TimeoutException ex) {
                Logger.log (LogLevel.EXCEPTION, " --{0}", ex);
            }
    }

}

No comments:

Post a Comment