net.refractions.udig.ui
Class UDIGDisplaySafeLock

java.lang.Object
  extended by net.refractions.udig.ui.UDIGDisplaySafeLock
All Implemented Interfaces:
java.util.concurrent.locks.Lock

public class UDIGDisplaySafeLock
extends java.lang.Object
implements java.util.concurrent.locks.Lock

This lock is reentrant and guarantees that a display thread will not block, it will continue to call Display#readAndDispatch(). The Display thread gets priority over non-display threads. API is copied from the ReentrantLock

Since:
1.1.0
Author:
Jesse

Constructor Summary
UDIGDisplaySafeLock()
           
 
Method Summary
 int getHoldCount()
          Queries the number of holds on this lock by the current thread.
 int getWaitQueueLength(java.util.concurrent.locks.Condition condition)
          Returns an estimate of the number of threads waiting on the given condition associated with this lock.
 boolean hasWaiters(java.util.concurrent.locks.Condition condition)
          Queries whether any threads are waiting on the given condition associated with this lock.
protected  void init()
           
 boolean isHeldByCurrentThread()
          Queries if this lock is held by the current thread.
 boolean isLocked()
          Returns true if locked by a different thread.
 void lock()
           
 void lockInterruptibly()
           
 java.util.concurrent.locks.Condition newCondition()
           
 boolean tryLock()
           
 boolean tryLock(long timeout, java.util.concurrent.TimeUnit unit)
           
 void unlock()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDIGDisplaySafeLock

public UDIGDisplaySafeLock()
Method Detail

init

protected void init()

getHoldCount

public int getHoldCount()
Queries the number of holds on this lock by the current thread.

A thread has a hold on a lock for each lock action that is not matched by an unlock action.

The hold count information is typically only used for testing and debugging purposes. For example, if a certain section of code should not be entered with the lock already held then we can assert that fact:

 class X {
   ReentrantLock lock = new ReentrantLock();
   // ...     
   public void m() { 
     assert lock.getHoldCount() == 0;
     lock.lock();
     try {
       // ... method body
     } finally {
       lock.unlock();
     }
   }
 }
 

Returns:
the number of holds on this lock by the current thread, or zero if this lock is not held by the current thread.

getWaitQueueLength

public int getWaitQueueLength(java.util.concurrent.locks.Condition condition)
Returns an estimate of the number of threads waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.

Parameters:
condition - the condition
Returns:
the estimated number of waiting threads.
Throws:
java.lang.IllegalMonitorStateException - if this lock is not held
java.lang.IllegalArgumentException - if the given condition is not associated with this lock
java.lang.NullPointerException - if condition null

hasWaiters

public boolean hasWaiters(java.util.concurrent.locks.Condition condition)
Queries whether any threads are waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that a future signal will awaken any threads. This method is designed primarily for use in monitoring of the system state.

Parameters:
condition - the condition
Returns:
true if there are any waiting threads.
Throws:
java.lang.IllegalMonitorStateException - if this lock is not held
java.lang.IllegalArgumentException - if the given condition is not associated with this lock
java.lang.NullPointerException - if condition null

isHeldByCurrentThread

public boolean isHeldByCurrentThread()
Queries if this lock is held by the current thread.

Analogous to the Thread.holdsLock(java.lang.Object) method for built-in monitor locks, this method is typically used for debugging and testing. For example, a method that should only be called while a lock is held can assert that this is the case:

 class X {
   ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() { 
       assert lock.isHeldByCurrentThread();
       // ... method body
   }
 }
 

It can also be used to ensure that a reentrant lock is used in a non-reentrant manner, for example:

 class X {
   ReentrantLock lock = new ReentrantLock();
   // ...

   public void m() { 
       assert !lock.isHeldByCurrentThread();
       lock.lock();
       try {
           // ... method body
       } finally {
           lock.unlock();
       }
   }
 }
 

Returns:
true if current thread holds this lock and false otherwise.

isLocked

public boolean isLocked()
Returns true if locked by a different thread.

Returns:
true if locked by a different thread.

lock

public void lock()
Specified by:
lock in interface java.util.concurrent.locks.Lock

lockInterruptibly

public void lockInterruptibly()
                       throws java.lang.InterruptedException
Specified by:
lockInterruptibly in interface java.util.concurrent.locks.Lock
Throws:
java.lang.InterruptedException

newCondition

public java.util.concurrent.locks.Condition newCondition()
Specified by:
newCondition in interface java.util.concurrent.locks.Lock

tryLock

public boolean tryLock()
Specified by:
tryLock in interface java.util.concurrent.locks.Lock

tryLock

public boolean tryLock(long timeout,
                       java.util.concurrent.TimeUnit unit)
                throws java.lang.InterruptedException
Specified by:
tryLock in interface java.util.concurrent.locks.Lock
Throws:
java.lang.InterruptedException

unlock

public void unlock()
Specified by:
unlock in interface java.util.concurrent.locks.Lock