com.atlassian.util.concurrent
Class BooleanLatch

java.lang.Object
  extended by com.atlassian.util.concurrent.BooleanLatch

@ThreadSafe
public class BooleanLatch
extends java.lang.Object

A BooleanLatch is a reusable latch that resets after it is released and waited on. It depends on a boolean condition of being released or not and becomes unreleased when one thread successfully awaits it. It is useful for rally like release-wait-release coordination, and as a replacement to waiting on a Condition (it should be faster as the write thread does not need to acquire a lock in order to signal.

This latch is suitable for SRSW coordination. MRSW is supported but has the same semantics as Condition.signal(), that is to say that Condition.signalAll() is not supported and if there are multiple waiters then the particular thread that is released is arbitrary.


Constructor Summary
BooleanLatch()
           
 
Method Summary
 void await()
          Causes the current thread to wait until the latch has been released, unless the thread is interrupted.
 boolean await(long timeout, java.util.concurrent.TimeUnit unit)
          Causes the current thread to wait until the latch has been released, unless the thread is interrupted, or the specified waiting time elapses.
 void release()
          Release the latch, releasing at most one waiting thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BooleanLatch

public BooleanLatch()
Method Detail

release

public void release()
Release the latch, releasing at most one waiting thread.

If the current state is released then nothing happens.


await

public void await()
           throws java.lang.InterruptedException
Causes the current thread to wait until the latch has been released, unless the thread is interrupted.

If the latch has already been released then this method returns immediately.

If the latch is not released then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:

If the current thread:

then InterruptedException is thrown and the current thread's interrupted status is cleared.

Throws:
java.lang.InterruptedException - if the current thread is interrupted while waiting

await

public boolean await(long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException
Causes the current thread to wait until the latch has been released, unless the thread is interrupted, or the specified waiting time elapses.

If the latch has already been released then this method returns immediately with return value true.

If the latch is unreleased then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happen:

If latch is released by another thread then the method returns with the value true.

If the current thread:

then InterruptedException is thrown and the current thread's interrupted status is cleared.

If the specified waiting time elapses then the value false is returned. If the time is less than or equal to zero, the method will not wait at all.

Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
true if the count reached zero and false if the waiting time elapsed before the count reached zero
Throws:
java.lang.InterruptedException - if the current thread is interrupted while waiting


Copyright © 2008 Atlassian Pty Ltd. All Rights Reserved.