Class Locks<TYPE>


  • public class Locks<TYPE>
    extends java.lang.Object
    Holds a map of locks indexed on keys of a given type. This is suitable in cases where exclusive access should be granted to any one of a set of keyed objects and there is a finite collection of keyed objects. The returned locks are reentrant (i.e the owning thread may call lock multiple times) and auto-closable. Typical use is try (Lock lock = locks.lock(id)) { exclusive use of the object with key id }
    Author:
    bratseth
    • Constructor Summary

      Constructors 
      Constructor Description
      Locks​(long timeout, java.util.concurrent.TimeUnit timeoutUnit)
      Create locks with a default timeout
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Lock lock​(TYPE key)
      Locks key.
      Lock lock​(TYPE key, long timeout, java.util.concurrent.TimeUnit timeoutUnit)
      Locks key.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Locks

        public Locks​(long timeout,
                     java.util.concurrent.TimeUnit timeoutUnit)
        Create locks with a default timeout
    • Method Detail

      • lock

        public Lock lock​(TYPE key)
        Locks key. This will block until the key is acquired or the default timeout is reached.
      • lock

        public Lock lock​(TYPE key,
                         long timeout,
                         java.util.concurrent.TimeUnit timeoutUnit)
        Locks key. This will block until the key is acquired or the timeout is reached. Users of this must close any lock acquired.
        Parameters:
        key - the key to lock
        Returns:
        the acquired lock
        Throws:
        com.google.common.util.concurrent.UncheckedTimeoutException - if the lock could not be acquired within the timeout