Top Description Inners Fields Constructors Methods
io.vavr.concurrent

pack-priv final Class FutureImpl<T>

extends Object
implements Future<T>
Class Inheritance
All Implemented Interfaces
io.vavr.concurrent.Future, io.vavr.Value, java.lang.Iterable
Annotations
@SuppressWarnings:deprecation
Type Parameters
<T>
Result of the computation.
Imports
io.vavr.collection.Queue, io.vavr.control.Option, .Try, java.util.Objects, java.util.concurrent.*, java.util.concurrent.locks.Lock, .LockSupport, .ReentrantLock, java.util.function.Consumer

INTERNAL API - This class is subject to change.
Author
Daniel Dietrich

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static interface

Field Summary

Modifier and TypeField and Description
private Queue<Consumer<Try<T>>>
actions

The queue of actions is filled when calling onComplete() before the Future is completed or cancelled.

private volatile boolean
cancelled

Indicates if this Future is cancelled GuardedBy("lock")

private final Executor
executor

Used to start new threads.

private final Lock
lock

Used to synchronize state changes.

private Thread
thread

The Thread which runs the computation.

private volatile Option<Try<T>>
value

Once the Future is completed, the value is defined.

private Queue<Thread>
waiters

The queue of waiters is filled when calling await() before the Future is completed or cancelled.

Constructor Summary

AccessConstructor and Description
private
FutureImpl(Executor executor, Option<Try<T>> value, Queue<Consumer<Try<T>>> actions, Queue<Thread> waiters, FutureImpl.Computation<T> computation)

Method Summary

Modifier and TypeMethod and Description
private void
_await(long
the start time in nanos, based on System#nanoTime()
start
,
long
a timeout in the given unit of time
timeout
,
TimeUnit
a time unit
unit
)

Blocks the current thread.

pack-priv static <
value type of the Future
T
>
FutureImpl<T>

Returns:

a new FutureImpl instance
async
(Executor
An Executor to run and control the computation and to perform the actions.
executor
,
Task<? extends T>
A (possibly blocking) computation
task
)
References Deprecated Task is deprecated or references (maybe indirectly) at least one deprecated element.

Creates a FutureImpl that is eventually completed.

public Future<T>
await()

Implements io.vavr.concurrent.Future.await.

Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

public Future<T>
await(long
the maximum time to wait
timeout
,
TimeUnit
the time unit of the timeout argument
unit
)

Implements io.vavr.concurrent.Future.await.

Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

public boolean
cancel(boolean
true if a running thread should be interrupted, otherwise a running thread is allowed to complete its computation.
mayInterruptIfRunning
)

Implements io.vavr.concurrent.Future.cancel.

Cancels the Future.

public Executor
executor()

Overrides default io.vavr.concurrent.Future.executor.

Returns the Executor used by this Future.

public ExecutorService
executorService()

Implements io.vavr.concurrent.Future.executorService.

Deprecated

This method is deprecated.

public Option<Try<T>>
getValue()

Implements io.vavr.concurrent.Future.getValue.

Returns the value of the Future.

private void
public boolean
isCancelled()

Implements io.vavr.concurrent.Future.isCancelled.

Checks if this Future is cancelled, i.e. the thread was forced to stop before completion.

public boolean
isCompleted()

Implements io.vavr.concurrent.Future.isCompleted.

Checks if this Future is completed, i.e. has a value.

pack-priv static <
value type of the Future
T
>
FutureImpl<T>

Returns:

a new FutureImpl instance
of
(Executor
An Executor to run and control the computation and to perform the actions.
executor
)

Creates a FutureImpl that needs to be automatically completed by calling tryComplete(Try).

pack-priv static <
value type of the Future
T
>
FutureImpl<T>

Returns:

a new FutureImpl instance
of
(Executor
An Executor to run and control the computation and to perform the actions.
executor
,
Try<? extends T>
the result of this Future
value
)

Creates a FutureImpl that is immediately completed with the given value.

public Future<T>
onComplete(Consumer<? super Try<T>>
An action to be performed when this future is complete.
action
)

Implements io.vavr.concurrent.Future.onComplete.

Performs the action once the Future is complete.

private void
perform(Consumer<? super Try<T>> action)

pack-priv static <
value type of the Future
T
>
FutureImpl<T>

Returns:

a new FutureImpl instance
sync
(Executor
An Executor to run and control the computation and to perform the actions.
executor
,
Task<? extends T>
A non-blocking computation
task
)
References Deprecated Task is deprecated or references (maybe indirectly) at least one deprecated element.

Creates a FutureImpl that is eventually completed.

public String
toString()

Overrides java.lang.Object.toString.

Implements io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

pack-priv boolean
tryComplete(Try<? extends T>
A Success containing a result or a Failure containing an Exception.
value
)

INTERNAL METHOD, SHOULD BE USED BY THE CONSTRUCTOR, ONLY.

private void
unlock(Thread waiter)

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

actionsback to summary
private Queue<Consumer<Try<T>>> actions

The queue of actions is filled when calling onComplete() before the Future is completed or cancelled. Otherwise actions = null. GuardedBy("lock")

cancelledback to summary
private volatile boolean cancelled

Indicates if this Future is cancelled GuardedBy("lock")

executorback to summary
private final Executor executor

Used to start new threads.

lockback to summary
private final Lock lock

Used to synchronize state changes.

threadback to summary
private Thread thread

The Thread which runs the computation. GuardedBy("lock")

valueback to summary
private volatile Option<Try<T>> value

Once the Future is completed, the value is defined. GuardedBy("lock")

waitersback to summary
private Queue<Thread> waiters

The queue of waiters is filled when calling await() before the Future is completed or cancelled. Otherwise waiters = null. GuardedBy("lock")

Constructor Detail

FutureImplback to summary
private FutureImpl(Executor executor, Option<Try<T>> value, Queue<Consumer<Try<T>>> actions, Queue<Thread> waiters, FutureImpl.Computation<T> computation)

Method Detail

_awaitback to summary
private void _await(long start, long timeout, TimeUnit unit)

Blocks the current thread.

If timeout = 0 then LockSupport.park() is called (start, timeout and unit are not used), otherwise LockSupport.park(timeout, unit} is called.

If a timeout > -1 is specified and the deadline is not met, this Future fails with a TimeoutException.

If this Thread was interrupted, this Future fails with a InterruptedException.

Parameters
start:long

the start time in nanos, based on System#nanoTime()

timeout:long

a timeout in the given unit of time

unit:TimeUnit

a time unit

asyncback to summary
pack-priv static <T> FutureImpl<T> async(Executor executor, Task<? extends T> task)

References Deprecated

Task is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Creates a FutureImpl that is eventually completed. The given computation is asynchronously executed, a new thread is started.

Parameters
<T>
value type of the Future
executor:Executor

An Executor to run and control the computation and to perform the actions.

task:Task<? extends T>

A (possibly blocking) computation

Returns:FutureImpl<T>

a new FutureImpl instance

awaitback to summary
public Future<T> await()

Implements io.vavr.concurrent.Future.await.

Doc from io.vavr.concurrent.Future.await.

Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

In the case the current thread was interrupted while waiting, a failed Future is returned containing the corresponding InterruptedException.

Returns:Future<T>

this Future instance

Annotations
@Override
awaitback to summary
public Future<T> await(long timeout, TimeUnit unit)

Implements io.vavr.concurrent.Future.await.

Doc from io.vavr.concurrent.Future.await.

Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

In the case the current thread was interrupted while waiting, a failed Future is returned containing the corresponding InterruptedException.

If the deadline wasn't met, a failed Future is returned containing a TimeoutException.

Parameters
timeout:long

the maximum time to wait

unit:TimeUnit

the time unit of the timeout argument

Returns:Future<T>

this Future instance

Annotations
@Override
cancelback to summary
public boolean cancel(boolean mayInterruptIfRunning)

Implements io.vavr.concurrent.Future.cancel.

Doc from io.vavr.concurrent.Future.cancel.

Cancels the Future. A pending Future may be interrupted, depending on the underlying Executor.

If the Future was successfully cancelled, the result is a Failure(CancellationException).

Parameters
mayInterruptIfRunning:boolean

true if a running thread should be interrupted, otherwise a running thread is allowed to complete its computation.

Returns:boolean

false, if this Future is already completed or could not be cancelled, otherwise true.

Annotations
@Override
executorback to summary
public Executor executor()

Overrides default io.vavr.concurrent.Future.executor.

Doc from io.vavr.concurrent.Future.executor.

Returns the Executor used by this Future.

Returns:Executor

The underlying Executor.

Annotations
@Override
executorServiceback to summary
public ExecutorService executorService()

Implements io.vavr.concurrent.Future.executorService.

Doc from io.vavr.concurrent.Future.executorService.

Deprecated

This method is deprecated.

THE DEFAULT IMPLEMENTATION (obtained by one of the Future factory methods) MIGHT THROW AN UnsupportedOperationException AT RUNTIME.

Returns:ExecutorService

(never)

Annotations
@Deprecated
@Override
getValueback to summary
public Option<Try<T>> getValue()

Implements io.vavr.concurrent.Future.getValue.

Doc from io.vavr.concurrent.Future.getValue.

Returns the value of the Future.

Returns:Option<Try<T>>

None, if the Future is not yet completed or was cancelled, otherwise Some(Try).

Annotations
@Override
handleUncaughtExceptionback to summary
private void handleUncaughtException(Throwable x)
isCancelledback to summary
public boolean isCancelled()

Implements io.vavr.concurrent.Future.isCancelled.

Doc from io.vavr.concurrent.Future.isCancelled.

Checks if this Future is cancelled, i.e. the thread was forced to stop before completion.

Returns:boolean

true, if the computation was cancelled, false otherwise

Annotations
@Override
isCompletedback to summary
public boolean isCompleted()

Implements io.vavr.concurrent.Future.isCompleted.

Doc from io.vavr.concurrent.Future.isCompleted.

Checks if this Future is completed, i.e. has a value.

Returns:boolean

true, if the computation successfully finished, failed or was cancelled, false otherwise.

Annotations
@Override
ofback to summary
pack-priv static <T> FutureImpl<T> of(Executor executor)

Creates a FutureImpl that needs to be automatically completed by calling tryComplete(Try).

Parameters
<T>
value type of the Future
executor:Executor

An Executor to run and control the computation and to perform the actions.

Returns:FutureImpl<T>

a new FutureImpl instance

ofback to summary
pack-priv static <T> FutureImpl<T> of(Executor executor, Try<? extends T> value)

Creates a FutureImpl that is immediately completed with the given value. No task will be started.

Parameters
<T>
value type of the Future
executor:Executor

An Executor to run and control the computation and to perform the actions.

value:Try<? extends T>

the result of this Future

Returns:FutureImpl<T>

a new FutureImpl instance

onCompleteback to summary
public Future<T> onComplete(Consumer<? super Try<T>> action)

Implements io.vavr.concurrent.Future.onComplete.

Doc from io.vavr.concurrent.Future.onComplete.

Performs the action once the Future is complete.

Parameters
action:Consumer<? super Try<T>>

An action to be performed when this future is complete.

Returns:Future<T>

this Future

Annotations
@SuppressWarnings:unchecked
@Override
performback to summary
private void perform(Consumer<? super Try<T>> action)
syncback to summary
pack-priv static <T> FutureImpl<T> sync(Executor executor, Task<? extends T> task)

References Deprecated

Task is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Creates a FutureImpl that is eventually completed. The given computation is synchronously executed, no thread is started.

Parameters
<T>
value type of the Future
executor:Executor

An Executor to run and control the computation and to perform the actions.

task:Task<? extends T>

A non-blocking computation

Returns:FutureImpl<T>

a new FutureImpl instance

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements io.vavr.Value.toString.

Doc from io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

See Object.toString().

Returns:String

A String representation of this object

Annotations
@Override
tryCompleteback to summary
pack-priv boolean tryComplete(Try<? extends T> value)

INTERNAL METHOD, SHOULD BE USED BY THE CONSTRUCTOR, ONLY.

Completes this Future with a value and performs all actions.

This method is idempotent. I.e. it does nothing, if this Future is already completed.

Parameters
value:Try<? extends T>

A Success containing a result or a Failure containing an Exception.

Exceptions
IllegalStateException:
if the Future is already completed or cancelled.
NullPointerException:
if the given value is null.
unlockback to summary
private void unlock(Thread waiter)
updateThreadback to summary
private void updateThread()
io.vavr.concurrent back to summary

private Interface FutureImpl.Computation<T>


Method Summary

Modifier and TypeMethod and Description
public void
execute(Task.Complete<T> complete, Runnable updateThread)

Method Detail

executeback to summary
public void execute(Task.Complete<T> complete, Runnable updateThread) throws Throwable