| 1 | package com.hammurapi.common.concurrent; |
| 2 | |
| 3 | import java.util.concurrent.Callable; |
| 4 | import java.util.concurrent.ExecutionException; |
| 5 | import java.util.concurrent.FutureTask; |
| 6 | |
| 7 | /** |
| 8 | * Future task which cannot block in get() indefinitely. If it is not yet started, |
| 9 | * the task gets executed in the caller thread. |
| 10 | * @author Pavel Vlasov |
| 11 | * |
| 12 | * @param <V> |
| 13 | */ |
| 14 | public class NonBlockingFutureTask<V> extends FutureTask<V> { |
| 15 | |
| 16 | public NonBlockingFutureTask(Callable<V> callable) { |
| 17 | super(callable); |
| 18 | } |
| 19 | |
| 20 | public NonBlockingFutureTask(Runnable runnable, V result) { |
| 21 | super(runnable, result); |
| 22 | } |
| 23 | |
| 24 | @Override |
| 25 | public V get() throws InterruptedException, ExecutionException { |
| 26 | if (!isDone()) { |
| 27 | run(); |
| 28 | } |
| 29 | return super.get(); |
| 30 | } |
| 31 | |
| 32 | } |