
delight.concurrency.utils.SharedExecutor Maven / Gradle / Ivy
Show all versions of delight-concurrency Show documentation
package delight.concurrency.utils;
import delight.async.callbacks.SimpleCallback;
import delight.concurrency.wrappers.SimpleExecutor;
import delight.functional.Function;
/**
*
* Allows multiple clients to share one executor.
*
* Useful to avoid creation/release of too many threads.
*
* @author Max Rohde
*
*/
public final class SharedExecutor {
private final Function executorFactory;
private volatile SimpleExecutor executor;
private Integer executorCount;
public SimpleExecutor reserveExecutor() {
synchronized (executorCount) {
executorCount += 1;
if (executor == null) {
executor = executorFactory.apply(null);
}
}
return executor;
}
public void releaseExecutor(final SimpleCallback callback) {
synchronized (executorCount) {
executorCount -= 1;
executor.shutdown(callback);
executor = null;
}
}
public SharedExecutor(final Function executorFactory) {
super();
this.executorFactory = executorFactory;
this.executorCount = 0;
}
}