PenProfile 2.x

com.livescribe.ext.util
Class Latch

java.lang.Object
  extended by com.livescribe.ext.util.Latch

public class Latch
extends Object

A latch that can be used for thread signalling.

Note: This uses the current instance for the wait/notify object.

Since:
2.4

Constructor Summary
Latch()
          Creates a new latch.
 
Method Summary
 boolean isSet()
          Checks if the latch is set.
 void resetLatch()
          Resets the latch.
 boolean setIfNotSet()
          Atomically sets the latch if it is not already set.
 void setLatch()
          Sets the latch.
 void waitLatch()
          Waits for the latch to be set.
 boolean waitLatch(long timeout)
          Waits for the latch to be set.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Latch

public Latch()
Creates a new latch. The latch will not be set.

Method Detail

setLatch

public void setLatch()
Sets the latch. If the latch is not already set, then any threads waiting for the latch to be set will be notified. Once the latch is set, it stays set until resetLatch() is called.

See Also:
resetLatch()

waitLatch

public void waitLatch()
               throws InterruptedException
Waits for the latch to be set. This returns immediately if the latch is already set.

Throws:
InterruptedException - if the thread was interrupted while waiting for the latch to be set.
See Also:
setLatch()

waitLatch

public boolean waitLatch(long timeout)
                  throws InterruptedException
Waits for the latch to be set. This returns immediately if the latch is already set.

This waits for the specified period of time. If the time elapses without the latch being set, then this returns false. Otherwise, if the latch is set before the time elapses, then this returns true.

As in the behaviour for Object.wait(long), a timeout of zero indicates that we should wait forever until notified or interrupted.

Parameters:
timeout - the maximum time to wait, in ms
Returns:
whether the latch was set before the timeout elapsed.
Throws:
InterruptedException - if the thread was interrupted while waiting for the latch to be set.
IllegalArgumentException - if the timeout value is negative.
See Also:
setLatch()

isSet

public boolean isSet()
Checks if the latch is set.

Returns:
whether the latch is set.

setIfNotSet

public boolean setIfNotSet()
Atomically sets the latch if it is not already set. This returns true if the latch state changed, and false otherwise.

Returns:
whether the latch state was changed.

resetLatch

public void resetLatch()
Resets the latch. The state is set to false. No threads are notified.

See Also:
setLatch()

PenProfile 2.x

Copyright © 2010 Livescribe Inc. All Rights Reserved.
Confidential and subject to NDA.