-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectPool.java
More file actions
37 lines (28 loc) · 740 Bytes
/
Copy pathObjectPool.java
File metadata and controls
37 lines (28 loc) · 740 Bytes
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
package creational.objectpool;
import java.util.HashSet;
import java.util.Set;
/**
* Created by Rana on 03/02/2022.
*/
public abstract class ObjectPool<T> {
private Set<T> availables;
private Set<T> uses;
public ObjectPool() {
availables = new HashSet<>();
uses = new HashSet<>();
}
protected abstract T create();
public synchronized T acquire() {
if (availables.isEmpty()) {
availables.add(create());
}
T object = availables.iterator().next();
availables.remove(object);
uses.add(object);
return object;
}
public synchronized void release(T object) {
uses.remove(object);
availables.add(object);
}
}