MCPcopy
hub / github.com/Alluxio/alluxio / ResourcePool

Class ResourcePool

core/common/src/main/java/alluxio/resource/ResourcePool.java:33–180  ·  view source on GitHub ↗

Class representing a pool of resources to be temporarily used and returned. Inheriting classes must implement the close method as well as initialize the resources in the constructor. The implemented methods are thread-safe and inheriting classes should also written in a thread-safe manner. See {@cod

Source from the content-addressed store, hash-verified

31 * @param <T> the type of resource this pool manages
32 */
33@ThreadSafe
34public abstract class ResourcePool<T> implements Pool<T> {
35 private static final long WAIT_INDEFINITELY = -1;
36 private final ReentrantLock mTakeLock;
37 private final Condition mNotEmpty;
38 protected final int mMaxCapacity;
39 protected final ConcurrentLinkedQueue<T> mResources;
40 /** It represents the total number of resources that have been created by this pool. */
41 protected final AtomicInteger mCurrentCapacity;
42
43 /**
44 * Creates a {@link ResourcePool} instance with the specified capacity.
45 *
46 * @param maxCapacity the maximum of resources in this pool
47 */
48 public ResourcePool(int maxCapacity) {
49 this(maxCapacity, new ConcurrentLinkedQueue<T>());
50 }
51
52 /**
53 * Internal constructor that can provide an object to be used for the internal queue.
54 *
55 * @param maxCapacity bhe maximum of resources in this pool
56 * @param resources blocking queue to use
57 */
58 protected ResourcePool(int maxCapacity, ConcurrentLinkedQueue<T> resources) {
59 mTakeLock = new ReentrantLock();
60 mNotEmpty = mTakeLock.newCondition();
61 mMaxCapacity = maxCapacity;
62 mCurrentCapacity = new AtomicInteger();
63 mResources = resources;
64 }
65
66 /**
67 * Acquires an object of type {@code T} from the pool. This operation is blocking if no resource
68 * is available. Each call of {@link #acquire()} should be paired with another call of
69 * {@link #release(Object)}} after the use of this resource completes to return this resource to
70 * the pool.
71 *
72 * @return a resource taken from the pool
73 */
74 @Override
75 public T acquire() {
76 return acquire(WAIT_INDEFINITELY, null);
77 }
78
79 /**
80 * Acquires an object of type {code T} from the pool.
81 *
82 * This method is like {@link #acquire()}, but it will time out if an object cannot be
83 * acquired before the specified amount of time.
84 *
85 * @param time an amount of time to wait, <= 0 to wait indefinitely
86 * @param unit the unit to use for time, null to wait indefinitely
87 * @return a resource taken from the pool, or null if the operation times out
88 */
89 @Override
90 @Nullable

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected