PenProfile 2.x

com.livescribe.ext.util
Class Lock

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

public class Lock
extends Object

Implementation of an exclusive lock. This is used by more than one thread to share a resource. The semantics are the same as Java mutexes. In other words, the lock is reentrant. A thread can reacquire the lock any number of times without blocking.

When acquiring a lock, it is a good idea to ensure it is released by putting it in a finally clause.

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

Since:
2.4

Constructor Summary
Lock()
          Creates a new lock object.
 
Method Summary
 void acquireLock()
          Tries to acquire the lock.
 boolean acquireLock(long timeout)
          Tries to acquire the lock.
 void releaseLock()
          Releases the lock if the current thread is the owner.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Lock

public Lock()
Creates a new lock object.

Method Detail

acquireLock

public void acquireLock()
                 throws InterruptedException
Tries to acquire the lock. The method blocks until the lock is acquired. This returns right away if the thread already owns the lock.

Throws:
InterruptedException - if this thread has been interrupted.

acquireLock

public boolean acquireLock(long timeout)
                    throws InterruptedException
Tries to acquire the lock. The method blocks until the lock is acquired, or until the timeout expires. This returns right away if the thread already owns the lock.

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 lock was acquired before the timeout elapsed.
Throws:
InterruptedException - if this thread was interrupted while waiting to acquire the lock.
IllegalArgumentException - if the timeout value is negative.

releaseLock

public void releaseLock()
Releases the lock if the current thread is the owner. All threads are woken up to compete for the lock.


PenProfile 2.x

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