Samples JDK
Barrier.java
1 package com.freemindcafe.concurrency.sample6;
2 
3 import java.util.concurrent.Semaphore;
4 
5 public class Barrier {
6 
7  private int nThreads;
8  private int counter;
9  private Semaphore semaphore = new Semaphore(0);
10  private Semaphore mutex = new Semaphore(1);
11 
12  public Barrier(int nThreads) {
13  this.counter = nThreads;
14  this.nThreads = nThreads;
15  }
16 
17  public void await() throws InterruptedException{
18  mutex.acquire();
19  counter--;
20  mutex.release();
21  if(counter == 0){
22  semaphore.release(nThreads);
23  }
24  semaphore.acquire();
25  }
26 
27 }