mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 16:58:47 +00:00
Reformat rest of the design patterns - Issue #224
This commit is contained in:
@@ -10,28 +10,28 @@ import java.util.HashSet;
|
||||
*/
|
||||
public abstract class ObjectPool<T> {
|
||||
|
||||
private HashSet<T> available = new HashSet<>();
|
||||
private HashSet<T> inUse = new HashSet<>();
|
||||
|
||||
protected abstract T create();
|
||||
|
||||
public synchronized T checkOut() {
|
||||
if (available.size() <= 0) {
|
||||
available.add(create());
|
||||
}
|
||||
T instance = available.iterator().next();
|
||||
available.remove(instance);
|
||||
inUse.add(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public synchronized void checkIn(T instance) {
|
||||
inUse.remove(instance);
|
||||
available.add(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
|
||||
}
|
||||
private HashSet<T> available = new HashSet<>();
|
||||
private HashSet<T> inUse = new HashSet<>();
|
||||
|
||||
protected abstract T create();
|
||||
|
||||
public synchronized T checkOut() {
|
||||
if (available.size() <= 0) {
|
||||
available.add(create());
|
||||
}
|
||||
T instance = available.iterator().next();
|
||||
available.remove(instance);
|
||||
inUse.add(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public synchronized void checkIn(T instance) {
|
||||
inUse.remove(instance);
|
||||
available.add(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Pool available=%d inUse=%d", available.size(), inUse.size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user