Implementation of barrier in Java
<p>How can a barrier be implemented <strong>with semaphores</strong> in Java. Will the following pseudo code work? How can it be written using java Semaphore class.</p>

<p><code>N</code> is the number of threads to be waited for at the barrier.
<code>EveryoneHasReachedBarrier</code> is a conditional variable.</p>

<pre><code>Aquire(mutex)
m = m + 1;
if(m != N)
{ 
    Release(mutex);
    Aquire(EveryoneHasReachedBarrier);
}
else
{
   m = 0;
   Release(mutex);
   for(i=0; i&lt;N; i++)
   {
       Release(EveryoneHasReachedBarrier);
   }
}
</code></pre>
