Class ResourceTable


  • public class ResourceTable
    extends java.lang.Object
    A table providing simple named locks for arbitrary resources.
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceTable()
      Create a resource table.
      ResourceTable​(int initialSize)
      Create a resource table of a specified size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean acquire​(java.lang.String[] resourceNames, int timeout)
      Try to acquire a set of named locks.
      void release​(java.lang.String... resourceNames)
      Release a set of previously acquired locks.
      java.util.Map<java.lang.String,​java.lang.Thread> table()
      Returns unmodifiable copy of the resource table.
      • Methods inherited from class java.lang.Object

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

      • ResourceTable

        public ResourceTable()
        Create a resource table.
      • ResourceTable

        public ResourceTable​(int initialSize)
        Create a resource table of a specified size.
        Parameters:
        initialSize - a hint as to the initial capacity to make the table
    • Method Detail

      • acquire

        public boolean acquire​(java.lang.String[] resourceNames,
                               int timeout)
                        throws java.lang.InterruptedException
        Try to acquire a set of named locks. To avoid deadlocks, the locks are acquired in a canonical order (alphabetical by name.)
        Parameters:
        resourceNames - a list of names identifying locks to be acquired.
        timeout - a maximum time, in milliseconds to ait for the locks to become available.
        Returns:
        true if and only if all the locks were successfully acquired
        Throws:
        java.lang.InterruptedException - is the method was interrupted while waiting for the locks to become available.
      • release

        public void release​(java.lang.String... resourceNames)
        Release a set of previously acquired locks. The named locks are only released if currently owned by the same thread that acquired them.
        Parameters:
        resourceNames - the names of the locks to be released
      • table

        public java.util.Map<java.lang.String,​java.lang.Thread> table()
        Returns unmodifiable copy of the resource table. Table contains resource names mapped to threads that originally called acquire method. Please note that query operations on the returned map "read through" to the internal original map which could be changing over time.
        Returns:
        unmodifiable resource table