Class LockPool<K>

java.lang.Object
ch.tocco.nice2.toolbox.api.LockPool<K>

public final class LockPool<K> extends Object
A pool of keyed locks. The pool makes sure that all user's share the same mapping from a key to a lock while allowing the locks to be garbage collected when unused. Note that using such a thing is bad practice, but may be useful in some seldom cases. ;)

Example Usage

     LockPool.Lock myLock = LockPool.lock("myKey"):
     try {
         // code
     }
     finally {
         myLock.unlock();
     }
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    A shared keyed lock.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a non-fair LockPool.
    LockPool(boolean fair)
    Creates a LockPool with the specified fairness.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(K key)
    Get or create a lock for the key without any action.
    lock(K key)
    Get or create a lock for the specified key and acquire the lock.
    Get or create a lock for the specified key and acquire the lock interruptibly.
    @Nullable LockPool.Lock
    tryLock(K key)
    Get or create a lock for the specified key and acquire the lock without waitinig.
    @Nullable LockPool.Lock
    tryLock(K key, long time, TimeUnit unit)
    Get or create a lock for the specified key and acquire the lock waiting until the specified timeout.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait