V
- The type of object returned by get()
public final class RepeatingFuture<V> extends Object implements IRepeatingFuture<V>
Future
of a task that is executed repeatedly but a limited
number of times.
The get()
and get(long, TimeUnit)
methods return the result of the most recent
execution and only block if the task has not yet run. This implementation does not support
changing the number of times to repeat the task.
Constructor and Description |
---|
RepeatingFuture(ITaskCanceller canceller,
int repetitions)
Constructor for the future.
|
Modifier and Type | Method and Description |
---|---|
protected void |
await()
Blocks the thread until the result is set or the task is cancelled.
|
protected boolean |
await(long timeout,
TimeUnit unit)
Blocks the thread until the result is set or the task is cancelled.
|
boolean |
cancel(boolean mayInterruptIfRunning) |
protected void |
checkCancellationException()
Checks if the
CancellationException needs to be thrown. |
protected void |
checkCancellationException(int index) |
protected void |
checkExecutionException(TaskExecutionResult<V> result)
Checks if a
ExecutionException needs to be thrown. |
V |
get() |
V |
get(long timeout,
TimeUnit unit) |
int |
getCompletedExecutions()
Returns the number of times the task has run this allows progress through the repetitions to
be monitored.
|
int |
getExpectedExecutions()
Get the number of executions that should happen.
|
protected TaskExecutionResult<V> |
getResult()
Return the result of the task execution.
|
V |
getResult(int index)
Waits if necessary for the ith computation to complete, and then retrieves its result.
|
V |
getResult(int index,
long timeout,
TimeUnit unit)
Waits if necessary for the ith computation to complete, and then retrieves its result.
|
protected TaskExecutionResult<V> |
getStoredResult(int index)
Gets the ith execution result.
|
boolean |
isCancelled() |
boolean |
isDone() |
protected boolean |
processCancellation(boolean mayInterruptIfRunning)
Process cancellation of the task.
|
protected void |
processResult(TaskExecutionResult<V> result)
Process the result being set.
|
void |
setException(Throwable result)
Set the result of the task execution.
|
void |
setResult(V result)
Set the result of the task execution.
|
void |
setTask(ITaskWrapper wrapper)
Set the task
|
protected boolean |
taskDone()
Indicate if the task has been executed.
|
public RepeatingFuture(ITaskCanceller canceller, int repetitions)
canceller
- A canceller for the futurerepetitions
- The number of times it is to repeatpublic V getResult(int index) throws InterruptedException, ExecutionException, CancellationException, IndexOutOfBoundsException
IRepeatingFuture
getResult
in interface IRepeatingFuture<V>
index
- The execution of the task to get the result forInterruptedException
- Thrown if the thread was interrupted while the waitingExecutionException
- Thrown if the execution of the task resulted in an exceptionCancellationException
- Thrown if the task was cancelled before reaching the ith executionIndexOutOfBoundsException
- Thrown if the ith value is greater than the number of executions or it is less
than 0.public V getResult(int index, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, CancellationException, TimeoutException, IndexOutOfBoundsException
IRepeatingFuture
getResult
in interface IRepeatingFuture<V>
index
- The execution of the task to get the result fortimeout
- The maximum time to waitunit
- The time unit of the timeout argumentInterruptedException
- Thrown if the thread was interrupted while the waitingExecutionException
- Thrown if the execution of the task resulted in an exceptionCancellationException
- Thrown if the task was cancelled before reaching the ith executionTimeoutException
- Thrown if the ith execution does not happen within the timeout periodIndexOutOfBoundsException
- Thrown if the ith value is greater than the number of executions or it is less
than 0.public int getExpectedExecutions()
IRepeatingFuture
getExpectedExecutions
in interface IRepeatingFuture<V>
public int getCompletedExecutions()
IRepeatingFuture
getCompletedExecutions
in interface IRepeatingFuture<V>
protected void checkCancellationException(int index) throws CancellationException
CancellationException
protected void processResult(TaskExecutionResult<V> result)
result
- The resultprotected boolean processCancellation(boolean mayInterruptIfRunning)
mayInterruptIfRunning
- If the thread should be interrupted if it is runningprotected boolean taskDone()
protected void await() throws InterruptedException
InterruptedException
- The thread was Interrupted while waitingprotected boolean await(long timeout, TimeUnit unit) throws InterruptedException
timeout
- Duration of the timeoutunit
- Unit of the timeoutInterruptedException
- The thread was Interrupted while waitingprotected TaskExecutionResult<V> getResult()
protected TaskExecutionResult<V> getStoredResult(int index)
index
- The result to getpublic void setTask(ITaskWrapper wrapper)
ISettableFuture
setTask
in interface ISettableFuture<V>
wrapper
- TaskWrapper for task of the futurepublic boolean cancel(boolean mayInterruptIfRunning)
public boolean isCancelled()
isCancelled
in interface Future<V>
public V get() throws InterruptedException, ExecutionException, CancellationException
get
in interface Future<V>
InterruptedException
ExecutionException
CancellationException
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException, CancellationException
get
in interface Future<V>
InterruptedException
ExecutionException
TimeoutException
CancellationException
protected void checkCancellationException() throws CancellationException
CancellationException
needs to be thrown.CancellationException
- If it needs to be thrownprotected void checkExecutionException(TaskExecutionResult<V> result) throws ExecutionException
ExecutionException
needs to be thrown.result
- The result of the taskExecutionException
- If it needs to be thrownpublic void setResult(V result)
ISettableFuture
setResult
in interface ISettableFuture<V>
result
- The result of the executionpublic void setException(Throwable result)
ISettableFuture
setException
in interface ISettableFuture<V>
result
- The execution thrown by the executionCopyright © 2016. All Rights Reserved.