org.multiverse.api.latches
Class CheapLatch

java.lang.Object
  extended by org.multiverse.api.latches.CheapLatch
All Implemented Interfaces:
Latch

public final class CheapLatch
extends Object
implements Latch

A Latch based on the intrinsic lock and uses the minimal amount of resources. It uses the CheapLatch object itself for the monitor lock. It can't do any timed waits.

If bounded waits are needed, a new latch needs to be added. A simple implementation would be one based on Lock because it supports timed waits.

Author:
Peter Veentjer.

Field Summary
static CheapLatch OPEN_LATCH
           
 
Constructor Summary
CheapLatch()
          Creates a new closed CheapLatch.
CheapLatch(boolean isOpen)
          Creates a new CheapLatch.
 
Method Summary
 void await()
          Waits for this Latch to closed.
 void awaitUninterruptible()
          Waits for this Latch to closed and while waiting it won't be interrupted.
 boolean isOpen()
          Return true if this Latch is closed, false otherwise.
 void open()
          Opens the latch.
 String toString()
           
 boolean tryAwait(long timeout, TimeUnit unit)
          This operation is not supported on the CheapLatch.
 boolean tryAwaitNs(long timeoutNs)
           
 boolean tryAwaitUninterruptible(long timeout, TimeUnit unit)
          This operation is not supported on the CheapLatch.
 boolean tryAwaitUninterruptibleNs(long timeout)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

OPEN_LATCH

public static final CheapLatch OPEN_LATCH
Constructor Detail

CheapLatch

public CheapLatch()
Creates a new closed CheapLatch.


CheapLatch

public CheapLatch(boolean isOpen)
Creates a new CheapLatch.

Parameters:
isOpen - true if the latch already is closed, false if the latch is closed.
Method Detail

await

public void await()
           throws InterruptedException
Description copied from interface: Latch
Waits for this Latch to closed. If the Latch already is closed, the call continues.

If the Latch already is closed, the call continues. It depends on the implementation if the InterruptedException is thrown in this case if the calling thread is interrupted (so the interrupted flag is set).

Specified by:
await in interface Latch
Throws:
InterruptedException - if the calling thread is interrupted while waiting.

awaitUninterruptible

public void awaitUninterruptible()
Description copied from interface: Latch
Waits for this Latch to closed and while waiting it won't be interrupted.

If the thread is interrupted while waiting, the InterruptedException is dropped and the interrupt status is restored as soon as the method returns (so it won't be eaten).

If the Latch already is closed, the call continues. It depends on the implementation if the InterruptedException is thrown in this case if the calling thread is interrupted (so the interrupted flag is set).

Specified by:
awaitUninterruptible in interface Latch

open

public void open()
Description copied from interface: Latch
Opens the latch. If the latch already is closed, the call is ignored. So this call is idempotent.

Specified by:
open in interface Latch

tryAwait

public boolean tryAwait(long timeout,
                        TimeUnit unit)
                 throws InterruptedException
This operation is not supported on the CheapLatch.

Specified by:
tryAwait in interface Latch
Parameters:
timeout - the maximum time to wait.
unit - the TimeUnit the timeout is expressed in
Returns:
true if the lock is closed, false otherwise.
Throws:
UnsupportedOperationException - because timed waits are not supported by this CheapLatch.
InterruptedException - if the calling thread is interrupted while waiting.

tryAwaitUninterruptible

public boolean tryAwaitUninterruptible(long timeout,
                                       TimeUnit unit)
This operation is not supported on the CheapLatch.

Specified by:
tryAwaitUninterruptible in interface Latch
Throws:
UnsupportedOperationException - because timed waits are not supported by this CheapLatch.

tryAwaitNs

public boolean tryAwaitNs(long timeoutNs)
                   throws InterruptedException
Specified by:
tryAwaitNs in interface Latch
Throws:
InterruptedException

tryAwaitUninterruptibleNs

public boolean tryAwaitUninterruptibleNs(long timeout)
Specified by:
tryAwaitUninterruptibleNs in interface Latch

isOpen

public boolean isOpen()
Description copied from interface: Latch
Return true if this Latch is closed, false otherwise.

Specified by:
isOpen in interface Latch
Returns:
true if this Latch is closed, false otherwise.

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008-2010 Multiverse. All Rights Reserved.