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

de.invation.code.toval.thread.SingleThreadExecutorService Maven / Gradle / Ivy

Go to download

TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.

The newest version!
package de.invation.code.toval.thread;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import de.invation.code.toval.validate.Validate;

public abstract class SingleThreadExecutorService implements CallableListener {
	
	private ExecutorService executorService = null;
	private Future futureResult = null;
	private Set> listeners = new HashSet>();
	private AbstractCallable callable;

	public SingleThreadExecutorService(){}
	
	public SingleThreadExecutorService(ExecutorListener listener){
		this();
		addExecutorListener(listener);
	}
	
	public void addExecutorListener(ExecutorListener listener){
		Validate.notNull(listener);
		listeners.add(listener);
	}
	
	public void removeExecutorListener(ExecutorListener listener){
		listeners.remove(listener);
	}
	
	public void setUpAndRun(){
		executorService = Executors.newSingleThreadExecutor();
		callable = createCallable();
		callable.addCallableListener(this);
		futureResult = executorService.submit(callable);
	}
	
	public boolean isDone(){
		return futureResult.isDone();
	}
	
	public void stop() throws Exception {
		executorService.shutdownNow();
		for(ExecutorListener listener: listeners)
			listener.executorStopped();
	}
	
	protected V getCallableResult() throws CancellationException, InterruptedException, ExecutionException {
		if(getCallable().getCallableResult() == null){
			futureResult.get();
		}
		return getCallable().getCallableResult();
	}
	
	public final Z getResult() throws E {
		try {
			return getResultFromCallableResult(getCallableResult());
		} catch (CancellationException e) {
			throw createException("Callable cancelled.", e);
		} catch (InterruptedException e) {
			throw createException("Callable interrupted.", e);
		} catch (ExecutionException e) {
			throw executionException(e);
		} catch(Exception e){
			throw createException("Exception while running callable.\n" + e.getMessage(), e);
		}
	}
	
	protected abstract E createException(String message, Throwable cause);
	
	protected abstract E executionException(ExecutionException e);
	
	protected abstract Z getResultFromCallableResult(V callableResult) throws Exception;
	
	@Override
	public void callableFinished(V result) {
		try {
			for (ExecutorListener listener : listeners)
				listener.executorFinished(getResultFromCallableResult(result));
		} catch (Exception e) {
			e.printStackTrace();
		}
		executorService.shutdownNow();
	}

	@Override
	public void callableStarted() {
		for(ExecutorListener listener: listeners)
			listener.executorStarted();
	}

	@Override
	public void callableStopped() {
		for(ExecutorListener listener: listeners)
			listener.executorStopped();
		executorService.shutdownNow();
	}

	@Override
	public void callableException(Exception e) {
		for(ExecutorListener listener: listeners)
			listener.executorException(e);
		executorService.shutdownNow();
	}

	protected abstract AbstractCallable createCallable();
	
	protected AbstractCallable getCallable(){
		return callable;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy