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

com.fathzer.games.util.exec.MultiThreadsContext Maven / Gradle / Ivy

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