
com.fathzer.games.util.exec.MultiThreadsContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of games-core Show documentation
Show all versions of games-core Show documentation
A core library to help implement two players games.
The newest version!
package com.fathzer.games.util.exec;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
class MultiThreadsContext> implements ExecutionContext {
protected final ContextualizedExecutor exec;
private final T globalContext;
MultiThreadsContext(T context, ContextualizedExecutor exec) {
this.exec = exec;
this.globalContext = context;
}
@Override
public T getContext() {
final T result = exec.getContext();
if (result==null) {
return globalContext;
}
return result;
}
@Override
public void execute(Collection tasks) {
Collection> callables = tasks.stream().map(this::toCallable).toList();
try {
final List> futures = exec.invokeAll(callables, globalContext);
exec.checkExceptions(futures);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private Callable toCallable(Runnable task) {
return () -> {
task.run();
return null;
};
}
@Override
public void close() {
exec.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy