Package sk.ipesoft.d2000.async.base
Class AdjustableFuture<V>
- java.lang.Object
-
- sk.ipesoft.d2000.async.base.AdjustableFuture<V>
-
- Type Parameters:
V
- type of the result
- All Implemented Interfaces:
java.util.concurrent.Future<V>
,FutureEvent<V>
public class AdjustableFuture<V> extends java.lang.Object implements FutureEvent<V>
Default implementation ofFutureEvent
.
-
-
Constructor Summary
Constructors Constructor Description AdjustableFuture()
Create new future.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addEventHandler(FutureEventHandler<V> futureEventHandler)
Add handler for handling result of the future.void
addEventHandler(FutureEventHandler<V> futureEventHandler, java.util.concurrent.Executor executor)
Add handler for handling result of the future and executor with thread for execution of handling.static FutureEvent<java.lang.Void>
allOf(java.util.Collection<? extends FutureEvent<?>> futures)
Create new instance that completes when all of futures are complete.static FutureEvent<java.lang.Void>
anyOf(java.util.Collection<? extends FutureEvent<?>> futures)
Create new instance of future that completes when first of futures completes.boolean
cancel(boolean force)
static <T> FutureEvent<T>
completedFuture(T value)
Create new instance with value already set.V
get()
V
get(long timeout, java.util.concurrent.TimeUnit units)
boolean
isCancelled()
boolean
isDone()
static <T> FutureEvent<T>
runTaskInNewThread(java.util.concurrent.Callable<T> task)
Create new thread, executetask
within the thread and obtain result as aFuture
object.static <T> FutureEvent<T>
runTaskInNewThread(java.util.concurrent.Callable<T> task, java.lang.String threadName)
Create new named thread, executetask
within the thread and obtain result as aFuture
object.void
setException(java.lang.Throwable error)
Set exception that has been thrown during evaluation of future result.void
setValue(V value)
Set value of future.
-
-
-
Method Detail
-
runTaskInNewThread
public static <T> FutureEvent<T> runTaskInNewThread(java.util.concurrent.Callable<T> task)
Create new thread, executetask
within the thread and obtain result as aFuture
object.- Type Parameters:
T
- type of the result- Parameters:
task
- callable to be executed- Returns:
- future object
-
runTaskInNewThread
public static <T> FutureEvent<T> runTaskInNewThread(java.util.concurrent.Callable<T> task, java.lang.String threadName)
Create new named thread, executetask
within the thread and obtain result as aFuture
object.- Type Parameters:
T
- type of the result- Parameters:
task
- callable to be executedthreadName
- name of new thread- Returns:
- future object
-
completedFuture
public static <T> FutureEvent<T> completedFuture(T value)
Create new instance with value already set.- Type Parameters:
T
- type of value- Parameters:
value
- value of future- Returns:
- completed future
-
allOf
public static FutureEvent<java.lang.Void> allOf(java.util.Collection<? extends FutureEvent<?>> futures)
Create new instance that completes when all of futures are complete.- Parameters:
futures
- collection of futures- Returns:
- new instance of future
-
anyOf
public static FutureEvent<java.lang.Void> anyOf(java.util.Collection<? extends FutureEvent<?>> futures)
Create new instance of future that completes when first of futures completes.- Parameters:
futures
- collection of future values- Returns:
- new instance
-
setValue
public void setValue(V value)
Set value of future.- Parameters:
value
- value of future result
-
setException
public void setException(java.lang.Throwable error)
Set exception that has been thrown during evaluation of future result.- Parameters:
error
- thrown exception
-
cancel
public boolean cancel(boolean force)
- Specified by:
cancel
in interfacejava.util.concurrent.Future<V>
-
get
public V get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
- Specified by:
get
in interfacejava.util.concurrent.Future<V>
- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
-
get
public V get(long timeout, java.util.concurrent.TimeUnit units) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
- Specified by:
get
in interfacejava.util.concurrent.Future<V>
- Throws:
java.lang.InterruptedException
java.util.concurrent.ExecutionException
java.util.concurrent.TimeoutException
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in interfacejava.util.concurrent.Future<V>
-
isDone
public boolean isDone()
- Specified by:
isDone
in interfacejava.util.concurrent.Future<V>
-
addEventHandler
public void addEventHandler(FutureEventHandler<V> futureEventHandler)
Description copied from interface:FutureEvent
Add handler for handling result of the future.In case that result of the future has been available prior this call, the
futureEventHandler
will be invoked immediately on calling thread. Otherwise it will be invoked when result of the future is available on thread that provided result of the future.Warning: handling of the result should be quick, because it can slow-down the system. Use
FutureEvent.addEventHandler(FutureEventHandler, Executor)
for adding long-running handlers.- Specified by:
addEventHandler
in interfaceFutureEvent<V>
- Parameters:
futureEventHandler
- handler of future result
-
addEventHandler
public void addEventHandler(FutureEventHandler<V> futureEventHandler, java.util.concurrent.Executor executor)
Description copied from interface:FutureEvent
Add handler for handling result of the future and executor with thread for execution of handling.In case that result of the future has been available prior this call, the
futureEventHandler
will be passed to executor immediately. Otherwise it will be passed when result of the future is available.- Specified by:
addEventHandler
in interfaceFutureEvent<V>
- Parameters:
futureEventHandler
- handler of future resultexecutor
- encapsulation of thread that will execute result's handling
-
-