com.atlassian.util.concurrent
Class BooleanLatch

java.lang.Object
  extended by com.atlassian.util.concurrent.BooleanLatch
All Implemented Interfaces:
Awaitable, ReusableLatch

@ThreadSafe
public class BooleanLatch
extends java.lang.Object
implements ReusableLatch

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()
          Await for the condition to become true. 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)
          Await for the specified time for the condition to become true. 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 one or more threads that are waiting on it. Releases 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 one or more threads that are waiting on it. Releases at most one waiting thread. If the current state is released then nothing happens.

Specified by:
release in interface ReusableLatch

await

public void await()
           throws java.lang.InterruptedException
Await for the condition to become true. 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.

Specified by:
await in interface Awaitable
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
Await for the specified time for the condition to become true. 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.

Specified by:
await in interface Awaitable
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 © 2009 Atlassian Pty Ltd. All Rights Reserved.