-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathResourcePool.java
55 lines (47 loc) · 1.48 KB
/
ResourcePool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package br.com.leonardoz.patterns.resource_pool;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* Pattern: Resource Pool
*
* Motivations: Some resources can be limited, and it's import to ensures those
* limits in concurrent programming.
*
* Intent: Establish a mechanism for limiting the resource use. It'll block the
* user when there's none available. It Implements the concurrent thread-safe
* pool using Semaphores.
*
* Applicability: Use when you want to create a pool of some limited resource.
*
*/
public class ResourcePool<T> {
private final static TimeUnit TIME_UNIT = TimeUnit.SECONDS;
private Semaphore semaphore;
private BlockingQueue<T> resources;
public ResourcePool(int poolSize, List<T> initializedResources) {
this.semaphore = new Semaphore(poolSize, true);
this.resources = new LinkedBlockingQueue<>(poolSize);
this.resources.addAll(initializedResources);
}
public T get() throws InterruptedException {
return get(Integer.MAX_VALUE);
}
public T get(long secondsToTimeout) throws InterruptedException {
semaphore.acquire();
try {
T resource = resources.poll(secondsToTimeout, TIME_UNIT);
return resource;
} finally {
semaphore.release();
}
}
public void release(T resource) throws InterruptedException {
if (resource != null) {
resources.put(resource);
semaphore.release();
}
}
}