Package org.apache.flink.util
Class ResourceGuard
- java.lang.Object
-
- org.apache.flink.util.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 theacquireResource()
call. As a result of the call, each client gets aResourceGuard.Lease
. Theclose()
method of the lease releases the resources and reduces the client count in theResourceGuard
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 toclose()
will block until the zero-open-leases condition is triggered. After the resource guard is closed, calls toacquireResource()
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ResourceGuard.Lease
A lease is issued by theResourceGuard
as result of calls toacquireResource()
.
-
Constructor Summary
Constructors Constructor Description ResourceGuard()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourceGuard.Lease
acquireResource()
Acquired access from one new client for the guarded resource.void
close()
Closed the resource guard.void
closeInterruptibly()
void
closeUninterruptibly()
If the current thread is interrupted while waiting for the close method to complete, then it will continue to wait.int
getLeaseCount()
Returns the current count of open leases.boolean
isClosed()
Returns true if the resource guard is closed, i.e. afterclose()
was called.
-
-
-
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.
-
closeInterruptibly
public void closeInterruptibly() throws InterruptedException
- Throws:
InterruptedException
-
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.
-
close
public void close()
Closed the resource guard. This method will block until all calls toacquireResource()
have seen their matching call toreleaseResource()
.- Specified by:
close
in interfaceAutoCloseable
-
isClosed
public boolean isClosed()
Returns true if the resource guard is closed, i.e. afterclose()
was called.
-
getLeaseCount
public int getLeaseCount()
Returns the current count of open leases.
-
-