All Downloads are FREE. Search and download functionalities are using the official Maven repository.

delight.concurrency.utils.SharedExecutor Maven / Gradle / Ivy

The newest version!
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; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy