com.github.romanqed.jutils.concurrent.ThreadTaskFabric Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jutils Show documentation
Show all versions of jutils Show documentation
A set of simple utilities for implementing missing functionality in different versions of Java.
package com.github.romanqed.jutils.concurrent;
import com.github.romanqed.jutils.util.Checks;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadTaskFabric implements TaskFabric {
protected ExecutorService executor;
public ThreadTaskFabric(ExecutorService executor) {
this.executor = Checks.requireNonNullElse(executor, Executors.newCachedThreadPool());
}
public ThreadTaskFabric() {
this(null);
}
@Override
public Task createTask(Callable action) {
return new AbstractTask() {
@Override
public T call() throws Exception {
return action.call();
}
@Override
public ExecutorService getExecutor() {
return executor;
}
};
}
@Override
public ExecutorService getExecutor() {
return executor;
}
@Override
public boolean hasExecutor() {
return executor != null;
}
@Override
public void close() {
executor.shutdown();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy