Example usage for java.util.concurrent CyclicBarrier CyclicBarrier

List of usage examples for java.util.concurrent CyclicBarrier CyclicBarrier

Introduction

In this page you can find the example usage for java.util.concurrent CyclicBarrier CyclicBarrier.

Prototype

public CyclicBarrier(int parties, Runnable barrierAction) 

Source Link

Document

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.

Usage

From source file:BarDemo.java

public static void main(String args[]) {
    CyclicBarrier cb = new CyclicBarrier(3, new BarAction());

    System.out.println("Starting");

    new MyThread(cb, "A");
    new MyThread(cb, "B");
    new MyThread(cb, "C");

}

From source file:BarDemo.java

public static void main(String args[]) {
    CyclicBarrier cb = new CyclicBarrier(3, new BarAction());

    System.out.println("Starting");

    new MyThread(cb, "A");
    new MyThread(cb, "B");
    new MyThread(cb, "C");

    new MyThread(cb, "X");
    new MyThread(cb, "Y");
    new MyThread(cb, "Z");

}

From source file:Worker.java

public static void main(String[] args) {
    Runnable barrierAction = () -> System.out.println("We are ready.");
    CyclicBarrier barrier = new CyclicBarrier(3, barrierAction);
    for (int i = 1; i <= 3; i++) {
        Worker t = new Worker(i, barrier);
        t.start();//  w w  w  .  ja va  2  s  .  c o  m
    }
}

From source file:Main.java

public void start(String[] args) {
    Runnable showResultsAction = new Runnable() {
        public void run() {
            showResults();/*from   ww w.  j  av  a2s . co  m*/
            results.clear();
        }
    };
    barrier = new CyclicBarrier(args.length, showResultsAction);

    for (final String site : args)
        new Thread() {
            public void run() {
                while (true) {
                    long time = timeConnect(site);
                    results.add(new Result(time, site));
                    try {
                        barrier.await();
                    } catch (BrokenBarrierException e) {
                        return;
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            }
        }.start();
}

From source file:Main.java

public void start(String[] args) {
    Runnable showResultsAction = new Runnable() {
        public void run() {
            showResults();/*from w  ww  .j a  v  a2 s. c  o  m*/
            results.clear();
        }
    };
    barrier = new CyclicBarrier(args.length, showResultsAction);

    for (final String site : args)
        new Thread() {
            public void run() {
                while (true) {
                    long time = timeConnect(site);
                    results.add(new Result(time, site));
                    try {
                        barrier.await(99, TimeUnit.SECONDS);
                    } catch (BrokenBarrierException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }.start();
}

From source file:Main.java

public void start(String[] args) {
    Runnable showResultsAction = new Runnable() {
        public void run() {
            showResults();//www  . j  a v a2  s.c o m
            results.clear();
        }
    };
    barrier = new CyclicBarrier(args.length, showResultsAction);

    for (final String site : args)
        new Thread() {
            public void run() {
                while (true) {
                    long time = timeConnect(site);
                    results.add(new Result(time, site));
                    try {
                        barrier.await(99, TimeUnit.SECONDS);
                        barrier.reset();
                    } catch (BrokenBarrierException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }.start();
}

From source file:Main.java

public void start(String[] args) {
    Runnable showResultsAction = new Runnable() {
        public void run() {
            showResults();//from w w  w.j a v  a  2  s . c o  m
            results.clear();
        }
    };
    barrier = new CyclicBarrier(args.length, showResultsAction);

    for (final String site : args)
        new Thread() {
            public void run() {
                while (true) {
                    long time = timeConnect(site);
                    results.add(new Result(time, site));
                    try {
                        barrier.await(99, TimeUnit.SECONDS);
                        System.out.println(barrier.isBroken());
                    } catch (BrokenBarrierException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }.start();
}

From source file:Main.java

public void start(String[] args) {
    Runnable showResultsAction = new Runnable() {
        public void run() {
            showResults();/*from  w w w.  j  av a  2s  . com*/
            results.clear();
        }
    };
    barrier = new CyclicBarrier(args.length, showResultsAction);

    for (final String site : args)
        new Thread() {
            public void run() {
                while (true) {
                    long time = timeConnect(site);
                    results.add(new Result(time, site));
                    try {
                        barrier.await(99, TimeUnit.SECONDS);
                        System.out.println(barrier.getParties());
                    } catch (BrokenBarrierException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }.start();
}

From source file:Main.java

public void start(String[] args) {
    Runnable showResultsAction = new Runnable() {
        public void run() {
            showResults();/*from  w  ww. ja v  a  2  s  . c  o m*/
            results.clear();
        }
    };
    barrier = new CyclicBarrier(args.length, showResultsAction);

    for (final String site : args)
        new Thread() {
            public void run() {
                while (true) {
                    long time = timeConnect(site);
                    results.add(new Result(time, site));
                    try {
                        barrier.await(99, TimeUnit.SECONDS);
                        System.out.println(barrier.getNumberWaiting());
                    } catch (BrokenBarrierException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }.start();
}

From source file:com.mgmtp.perfload.core.client.util.concurrent.DelayingExecutorServiceTest.java

@Test
public void testWithoutDelay() throws InterruptedException, BrokenBarrierException {
    DelayingExecutorService execSrv = new DelayingExecutorService();

    final StopWatch sw = new StopWatch();

    final CyclicBarrier stopBarrier = new CyclicBarrier(11, new Runnable() {
        @Override/*from  ww w .j  a  va2s  . c  o  m*/
        public void run() {
            sw.stop();
        }
    });

    sw.start();

    for (int i = 0; i < 10; ++i) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1L);
                    stopBarrier.await();
                } catch (Exception ex) {
                    throw new AssertionError(ex);
                }
            }
        };

        ScheduledFuture<?> future = execSrv.schedule(r, 0L, TimeUnit.NANOSECONDS);

        // compare with epsilon to make up for bad accuracy
        assertTrue(abs(future.getDelay(TimeUnit.MILLISECONDS)) < EPSILON);
    }

    stopBarrier.await();

    assertTrue(sw.getTime() < EPSILON);
}