Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.BrokenBarrierException;
- import java.util.concurrent.CyclicBarrier;
- public class TestCyclicBarrier {
- private static CyclicBarrier barrier = new CyclicBarrier(6);
- private static class printInt implements Runnable{
- private int i;
- public printInt(int i){
- this.i = i;
- }
- public void run() {
- System.out.println(i);
- try {
- barrier.await();
- } catch (InterruptedException | BrokenBarrierException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static void printInts() throws InterruptedException, BrokenBarrierException{
- while(true){
- for(int i = 0 ; i < 5 ; i++){
- new Thread(new printInt(i)).start();
- }
- barrier.await();
- System.out.println("done");
- Thread.sleep(2000);
- }
- }
- public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
- printInts();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement