com.meliorbis.numerics.threading.CurrentThreadExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Numerics Show documentation
Show all versions of Numerics Show documentation
A library for working with large multi-dimensional arrays and the functions they represent
package com.meliorbis.numerics.threading;
import java.util.List;
import java.util.stream.Collectors;
import com.meliorbis.numerics.NumericsException;
/**
* Runs all tasks passed on the calling thread, i.e. singlethreaded
*
* @author Tobias Grasl
*/
public class CurrentThreadExecutor implements Executor {
@Override
public void executeAndWait(List extends ComputableRecursiveAction> actions_) throws NumericsException
{
actions_.forEach(action -> {
action.compute();
});
}
@Override
public List executeAndGet(List extends ComputableRecursiveTask> tasks_)
throws NumericsException {
// Holds the last exception thrown
final Exception[] thrown = new Exception[1];
List results = tasks_.stream().map(task -> {
try
{
return task.compute();
} catch (Exception e)
{
thrown[0] = e;
return null;
}
}).collect(Collectors.toList());
// If am exception was thrown, pass it on
if(thrown[0] != null) {
throw new NumericsException(thrown[0]);
}
return results;
}
@Override
public void destroy() {
}
}