package com.hammurapi.common.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Future task which cannot block in get() indefinitely. If it is not yet started,
 * the task gets executed in the caller thread. 
 * @author Pavel Vlasov
 *
 * @param <V>
 */
public class NonBlockingFutureTask<V> extends FutureTask<V> {
	
	public NonBlockingFutureTask(Callable<V> callable) {
		super(callable);	
	}
	
	public NonBlockingFutureTask(Runnable runnable, V result) {
		super(runnable, result);
	}
		
	@Override
	public V get() throws InterruptedException, ExecutionException {
		if (!isDone()) {
			run();
		}
		return super.get();
	}
	
}
