Class Locks<TYPE>


  • public class Locks<TYPE>
    extends 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 Detail

      • Locks

        public Locks​(long timeout,
                     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,
                         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