Class ResourceGuard

  • All Implemented Interfaces:
    Serializable, AutoCloseable

    public class ResourceGuard
    extends Object
    implements AutoCloseable, Serializable
    This class is a guard for shared resources with the following invariants. The resource can be acquired by multiple clients in parallel through the acquireResource() call. As a result of the call, each client gets a ResourceGuard.Lease. The close() method of the lease releases the resources and reduces the client count in the ResourceGuard object. The protected resource should only be disposed once the corresponding resource guard is successfully closed, but the guard can only be closed once all clients that acquired a lease for the guarded resource have released it. Before this is happened, the call to close() will block until the zero-open-leases condition is triggered. After the resource guard is closed, calls to acquireResource() will fail with exception. Notice that, obviously clients are responsible to release the resource after usage. All clients are considered equal, i.e. there is only a global count maintained how many times the resource was acquired but not by whom.
    See Also:
    Serialized Form
    • Constructor Detail

      • ResourceGuard

        public ResourceGuard()
    • Method Detail

      • acquireResource

        public ResourceGuard.Lease acquireResource()
                                            throws IOException
        Acquired access from one new client for the guarded resource.
        Throws:
        IOException - when the resource guard is already closed.
      • closeUninterruptibly

        public void closeUninterruptibly()
        If the current thread is interrupted while waiting for the close method to complete, then it will continue to wait. When the thread does return from this method its interrupt status will be set.
      • isClosed

        public boolean isClosed()
        Returns true if the resource guard is closed, i.e. after close() was called.
      • getLeaseCount

        public int getLeaseCount()
        Returns the current count of open leases.