Advertisement
Guest User

Untitled

a guest
Feb 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.concurrent.BrokenBarrierException;
  2. import java.util.concurrent.CyclicBarrier;
  3.  
  4. public class TestCyclicBarrier {
  5.  
  6.     private static CyclicBarrier barrier = new CyclicBarrier(6);
  7.    
  8.     private static class printInt implements Runnable{
  9.         private int i;
  10.         public printInt(int i){
  11.             this.i = i;
  12.         }
  13.         public void run() {
  14.             System.out.println(i);
  15.             try {
  16.                 barrier.await();
  17.             } catch (InterruptedException | BrokenBarrierException e) {
  18.                 // TODO Auto-generated catch block
  19.                 e.printStackTrace();
  20.             }
  21.         }
  22.     }
  23.    
  24.     public static void printInts() throws InterruptedException, BrokenBarrierException{
  25.         while(true){
  26.             for(int i = 0 ; i < 5 ; i++){
  27.                 new Thread(new printInt(i)).start();
  28.             }
  29.             barrier.await();
  30.             System.out.println("done");
  31.             Thread.sleep(2000);
  32.         }
  33.     }
  34.    
  35.     public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
  36.         printInts();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement