org.nassimus.thread.util.SimpleObjectPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of FlowControl Show documentation
Show all versions of FlowControl Show documentation
Powerful Thread Pool Executor with Flow Control and BatchBuffer execution
package org.nassimus.thread.util;
import java.util.HashMap;
import java.util.Map;
public abstract class SimpleObjectPool {
private final int maxNbObjects;
private final Map objects;
private T[] objectsArray;
public SimpleObjectPool(int maxNbObjects){
this.maxNbObjects = maxNbObjects;
objects = new HashMap<>(maxNbObjects);
}
public abstract T createAnObject();
public T checkOut() {
if (objectsArray!=null)
for (T object : objectsArray) {
Boolean state = objects.get(object);
if (state!=null && !state) {
objects.put(object, true);
return object;
}
}
T object = createAnObject();
objects.put(object, true);
synchronized (this) {
objectsArray = (T[]) objects.keySet().toArray();
}
return object;
}
public void release(T object) {
synchronized (this){
objects.put(object, false);
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy