    001package com.hammurapi.common.concurrent;
    002
    003import java.util.concurrent.Callable;
    004import java.util.concurrent.ExecutionException;
    005import java.util.concurrent.FutureTask;
    006
    007/**
    008 * Future task which cannot block in get() indefinitely. If it is not yet started,
    009 * the task gets executed in the caller thread.
    010 * @author Pavel Vlasov
    011 *
    012 * @param <V>
    013 */
    014public class NonBlockingFutureTask<V> extends FutureTask<V> {
    015
    016        public NonBlockingFutureTask(Callable<V> callable) {
    017                super(callable);
    018        }
    019
    020        public NonBlockingFutureTask(Runnable runnable, V result) {
    021                super(runnable, result);
    022        }
    023
    024        @Override
    025        public V get() throws InterruptedException, ExecutionException {
    026                if (!isDone()) {
    027                        run();
    028                }
    029                return super.get();
    030        }
    031
    032}