org.apache.ignite.internal.util.future

## Class GridFutureAdapter<R>

- java.lang.Object
- - java.util.concurrent.locks.AbstractOwnableSynchronizer
  - - java.util.concurrent.locks.AbstractQueuedSynchronizer
    - - org.apache.ignite.internal.util.future.GridFutureAdapter<R>

All Implemented Interfaces:
  Externalizable, Serializable, IgniteInternalFuture<R>

Direct Known Subclasses:
  ComputeTaskInternalFuture, GridCacheMultiTxFuture, GridCacheQueryFutureAdapter, GridCompoundFuture, GridDhtAssignmentFetchFuture, GridDhtAtomicUpdateFuture, GridDhtPartitionsExchangeFuture, GridEmbeddedFuture, GridLocalLockFuture, GridNearAtomicUpdateFuture, GridServiceDeploymentFuture, GridStreamerStageExecutionFuture, GridWorkerFuture

    public class GridFutureAdapter<R>
    extends AbstractQueuedSynchronizer
    implements IgniteInternalFuture<R>, Externalizable

Future adapter.

See Also:
  Serialized Form

### Nested Class Summary

### Nested classes/interfaces inherited from class java.util.concurrent.locks.AbstractQueuedSynchronizer

`AbstractQueuedSynchronizer.ConditionObject`

### Field Summary

Fields
`protected GridKernalContext` `ctx` Context.
`protected static IgniteLogger` `log` Logger.

### Constructor Summary

Constructors
Constructor and Description
`GridFutureAdapter()` Empty constructor required for `Externalizable`.
`GridFutureAdapter(GridKernalContext ctx)`
`GridFutureAdapter(GridKernalContext ctx, boolean syncNotify)`

### Method Summary

Methods
`boolean` `cancel()` Default no-op implementation that always returns `false`.
`<T> IgniteInternalFuture<T>` `chain(IgniteClosure<? super IgniteInternalFuture<R>,T> doneCb)` Make a chained future to convert result of this future (when complete) into a new format.
`protected void` `checkValid()` Checks that future is in usable state.
`boolean` `concurrentNotify()` Gets value concurrent listener notification flag.
`void` `concurrentNotify(boolean concurNotify)` Flag to turn on or off concurrent listener notification.
`long` `duration()` Gets duration in milliseconds between start of the future and current time if future is not finished, or between start and finish of this future.
`long` `endTime()`
`protected Throwable` `error()`
`R` `get()` Synchronously waits for completion of the computation and returns computation result.
`R` `get(long timeout)` Synchronously waits for completion of the computation for up to the timeout specified and returns computation result.
`R` `get(long timeout, TimeUnit unit)` Synchronously waits for completion of the computation for up to the timeout specified and returns computation result.
`protected R` `get0(long nanosTimeout)`
`boolean` `isCancelled()` Returns `true` if this computation was cancelled before it completed normally.
`boolean` `isDone()` Checks if computation is done.
`boolean` `isFailed()`
`protected boolean` `isValid()`
`void` `listenAsync(IgniteInClosure<? super IgniteInternalFuture<R>> lsnr)` Registers listener closure to be asynchronously notified whenever future completes.
`boolean` `onCancelled()` Callback to notify that future is cancelled.
`boolean` `onDone()` Callback to notify that future is finished with `null` result.
`boolean` `onDone(R res)` Callback to notify that future is finished.
`boolean` `onDone(R res, Throwable err)` Callback to notify that future is finished.
`boolean` `onDone(Throwable err)` Callback to notify that future is finished.
`void` `readExternal(ObjectInput in)`
`protected R` `result()`
`long` `startTime()` Gets start time for this future.
`void` `stopListenAsync(IgniteInClosure<? super IgniteInternalFuture<R>>... lsnr)` Removes given listeners from the future.
`boolean` `syncNotify()` Gets value of synchronous listener notification flag.
`void` `syncNotify(boolean syncNotify)` Flag to turn on or off synchronous listener notification.
`String` `toString()`
`protected int` `tryAcquireShared(int ignore)`
`protected boolean` `tryReleaseShared(int ignore)`
`void` `writeExternal(ObjectOutput out)`

### Methods inherited from class java.util.concurrent.locks.AbstractQueuedSynchronizer

`acquire, acquireInterruptibly, acquireShared, acquireSharedInterruptibly, compareAndSetState, getExclusiveQueuedThreads, getFirstQueuedThread, getQueuedThreads, getQueueLength, getSharedQueuedThreads, getState, getWaitingThreads, getWaitQueueLength, hasContended, hasQueuedPredecessors, hasQueuedThreads, hasWaiters, isHeldExclusively, isQueued, owns, release, releaseShared, setState, tryAcquire, tryAcquireNanos, tryAcquireSharedNanos, tryRelease`

### Methods inherited from class java.util.concurrent.locks.AbstractOwnableSynchronizer

`getExclusiveOwnerThread, setExclusiveOwnerThread`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### log

    protected static IgniteLogger log

Logger.

#### ctx

    protected GridKernalContext ctx

Context.

### Constructor Detail

#### GridFutureAdapter

    public GridFutureAdapter()

Empty constructor required for `Externalizable`.

#### GridFutureAdapter

    public GridFutureAdapter(GridKernalContext ctx)

Parameters:
  `ctx` - Kernal context.

#### GridFutureAdapter

    public GridFutureAdapter(GridKernalContext ctx,
                     boolean syncNotify)

Parameters:
  `syncNotify` - Synchronous notify flag.
  `ctx` - Kernal context.

### Method Detail

#### startTime

    public long startTime()

Gets start time for this future.

Specified by:
  `startTime` in interface `IgniteInternalFuture<R>`
Returns:
  Start time for this future.

#### duration

    public long duration()

Gets duration in milliseconds between start of the future and current time if future is not finished, or between start and finish of this future.

Specified by:
  `duration` in interface `IgniteInternalFuture<R>`
Returns:
  Time in milliseconds this future has taken to execute.

#### endTime

    public long endTime()

Returns:
  Future end time.

#### concurrentNotify

    public boolean concurrentNotify()

Gets value concurrent listener notification flag. This flag comes into play only when a future has more than one listener subscribed to it. If this flag is `true`, then all listeners will be notified concurrently by different threads; otherwise, listeners will be notified one after another within one thread (depending on `IgniteInternalFuture.syncNotify()` flag, these notifications may happen either in the same thread which started the future, or in a different thread).

Default value is `false`. To change the default, set `IgniteSystemProperties.IGNITE_FUT_CONCURRENT_NOTIFICATION` system property to `true`.

Specified by:
  `concurrentNotify` in interface `IgniteInternalFuture<R>`
Returns:
  Concurrent listener notification flag

#### concurrentNotify

    public void concurrentNotify(boolean concurNotify)

Flag to turn on or off concurrent listener notification. This flag comes into play only when a future has more than one listener subscribed to it. If this flag is `true`, then all listeners will be notified concurrently by different threads; otherwise, listeners will be notified one after another within one thread (depending on `IgniteInternalFuture.syncNotify()` flag, these notifications may happen either in the same thread which started the future, or in a different thread).

Default value is `false`. To change the default, set `IgniteSystemProperties.IGNITE_FUT_CONCURRENT_NOTIFICATION` system property to `true`.

Specified by:
  `concurrentNotify` in interface `IgniteInternalFuture<R>`
Parameters:
  `concurNotify` - Flag to turn on or off concurrent listener notification.

#### syncNotify

    public boolean syncNotify()

Gets value of synchronous listener notification flag. If this flag is `true`, then upon future completion the notification may happen in the same thread that created the future. This becomes especially important when adding listener to a future that is already `done` - if this flag is `true`, then listener will be immediately notified within the same thread.

Default value is `false`. To change the default, set `IgniteSystemProperties.IGNITE_FUT_SYNC_NOTIFICATION` system property to `true`.

Specified by:
  `syncNotify` in interface `IgniteInternalFuture<R>`
Returns:
  Synchronous listener notification flag.

#### syncNotify

    public void syncNotify(boolean syncNotify)

Flag to turn on or off synchronous listener notification. If this flag is `true`, then upon future completion the notification may happen in the same thread that created the future. This becomes especially important when adding listener to a future that is already `done` - if this flag is `true`, then listener will be immediately notified within the same thread.

Default value is `false`. To change the default, set `IgniteSystemProperties.IGNITE_FUT_SYNC_NOTIFICATION` system property to `true`.

Specified by:
  `syncNotify` in interface `IgniteInternalFuture<R>`
Parameters:
  `syncNotify` - Flag to turn on or off synchronous listener notification.

#### checkValid

    protected void checkValid()

Checks that future is in usable state.

#### isValid

    protected boolean isValid()

Returns:
  Valid flag.

#### error

    protected Throwable error()

Returns:
  Value of error.

#### result

    protected R result()

Returns:
  Value of result.

#### get

    public R get()
          throws IgniteCheckedException

Synchronously waits for completion of the computation and returns computation result.

Specified by:
  `get` in interface `IgniteInternalFuture<R>`
Returns:
  Computation result.
Throws:
  `IgniteInterruptedCheckedException` - Subclass of `IgniteCheckedException` thrown if the wait was interrupted.
  `IgniteFutureCancelledCheckedException` - Subclass of `IgniteCheckedException` throws if computation was cancelled.
  `IgniteCheckedException` - If computation failed.

#### get

    public R get(long timeout)
          throws IgniteCheckedException

Synchronously waits for completion of the computation for up to the timeout specified and returns computation result. This method is equivalent to calling `get(long, TimeUnit.MILLISECONDS)`.

Specified by:
  `get` in interface `IgniteInternalFuture<R>`
Parameters:
  `timeout` - The maximum time to wait in milliseconds.
Returns:
  Computation result.
Throws:
  `IgniteInterruptedCheckedException` - Subclass of `IgniteCheckedException` thrown if the wait was interrupted.
  `IgniteFutureTimeoutCheckedException` - Subclass of `IgniteCheckedException` thrown if the wait was timed out.
  `IgniteFutureCancelledCheckedException` - Subclass of `IgniteCheckedException` throws if computation was cancelled.
  `IgniteCheckedException` - If computation failed.

#### get

    public R get(long timeout,
        TimeUnit unit)
          throws IgniteCheckedException

Synchronously waits for completion of the computation for up to the timeout specified and returns computation result.

Specified by:
  `get` in interface `IgniteInternalFuture<R>`
Parameters:
  `timeout` - The maximum time to wait.
  `unit` - The time unit of the `timeout` argument.
Returns:
  Computation result.
Throws:
  `IgniteInterruptedCheckedException` - Subclass of `IgniteCheckedException` thrown if the wait was interrupted.
  `IgniteFutureTimeoutCheckedException` - Subclass of `IgniteCheckedException` thrown if the wait was timed out.
  `IgniteFutureCancelledCheckedException` - Subclass of `IgniteCheckedException` throws if computation was cancelled.
  `IgniteCheckedException` - If computation failed.

#### get0

    @Nullable
    protected R get0(long nanosTimeout)
              throws InterruptedException,
                     IgniteCheckedException

Parameters:
  `nanosTimeout` - Timeout (nanoseconds).
Returns:
  Result.
Throws:
  `InterruptedException` - If interrupted.
  `IgniteFutureTimeoutCheckedException` - If timeout reached before computation completed.
  `IgniteCheckedException` - If error occurred.

#### listenAsync

    public void listenAsync(@Nullable
                   IgniteInClosure<? super IgniteInternalFuture<R>> lsnr)

Registers listener closure to be asynchronously notified whenever future completes.

Specified by:
  `listenAsync` in interface `IgniteInternalFuture<R>`
Parameters:
  `lsnr` - Listener closure to register. If not provided - this method is no-op.

#### stopListenAsync

    public void stopListenAsync(@Nullable
                       IgniteInClosure<? super IgniteInternalFuture<R>>... lsnr)

Removes given listeners from the future. If no listener is passed in, then all listeners will be removed.

Specified by:
  `stopListenAsync` in interface `IgniteInternalFuture<R>`
Parameters:
  `lsnr` - Listeners to remove.

#### chain

    public <T> IgniteInternalFuture<T> chain(IgniteClosure<? super IgniteInternalFuture<R>,T> doneCb)

Make a chained future to convert result of this future (when complete) into a new format. It is guaranteed that done callback will be called only ONCE.

Specified by:
  `chain` in interface `IgniteInternalFuture<R>`
Parameters:
  `doneCb` - Done callback that is applied to this future when it finishes to produce chained future result.
Returns:
  Chained future that finishes after this future completes and done callback is called.

#### cancel

    public boolean cancel()
                   throws IgniteCheckedException

Default no-op implementation that always returns `false`. Futures that do support cancellation should override this method and call `onCancelled()` callback explicitly if cancellation indeed did happen.

Specified by:
  `cancel` in interface `IgniteInternalFuture<R>`
Returns:
  `True` if future was canceled (i.e. was not finished prior to this call).
Throws:
  `IgniteCheckedException` - If cancellation failed.

#### isDone

    public boolean isDone()

Checks if computation is done.

Specified by:
  `isDone` in interface `IgniteInternalFuture<R>`
Returns:
  `True` if computation is done, `false` otherwise.

#### isFailed

    public boolean isFailed()

Returns:
  Checks is future is completed with exception.

#### isCancelled

    public boolean isCancelled()

Returns `true` if this computation was cancelled before it completed normally.

Specified by:
  `isCancelled` in interface `IgniteInternalFuture<R>`
Returns:
  `True` if this computation was cancelled before it completed normally.

#### onDone

    public final boolean onDone()

Callback to notify that future is finished with `null` result. This method must delegate to `onDone(Object, Throwable)` method.

Returns:
  `True` if result was set by this call.

#### onDone

    public final boolean onDone(@Nullable
                 R res)

Callback to notify that future is finished. This method must delegate to `onDone(Object, Throwable)` method.

Parameters:
  `res` - Result.
Returns:
  `True` if result was set by this call.

#### onDone

    public final boolean onDone(@Nullable
                 Throwable err)

Callback to notify that future is finished. This method must delegate to `onDone(Object, Throwable)` method.

Parameters:
  `err` - Error.
Returns:
  `True` if result was set by this call.

#### onDone

    public boolean onDone(@Nullable
                 R res,
                 @Nullable
                 Throwable err)

Callback to notify that future is finished. Note that if non-`null` exception is passed in the result value will be ignored.

Parameters:
  `res` - Optional result.
  `err` - Optional error.
Returns:
  `True` if result was set by this call.

#### onCancelled

    public boolean onCancelled()

Callback to notify that future is cancelled.

Returns:
  `True` if cancel flag was set by this call.

#### tryAcquireShared

    protected final int tryAcquireShared(int ignore)

Overrides:
  `tryAcquireShared` in class `AbstractQueuedSynchronizer`

#### tryReleaseShared

    protected final boolean tryReleaseShared(int ignore)

Overrides:
  `tryReleaseShared` in class `AbstractQueuedSynchronizer`

#### writeExternal

    public void writeExternal(ObjectOutput out)
                       throws IOException

Specified by:
  `writeExternal` in interface `Externalizable`
Throws:
  `IOException`

#### readExternal

    public void readExternal(ObjectInput in)
                      throws IOException,
                             ClassNotFoundException

Specified by:
  `readExternal` in interface `Externalizable`
Throws:
  `IOException`
  `ClassNotFoundException`

#### toString

    public String toString()

Overrides:
  `toString` in class `AbstractQueuedSynchronizer`

2015 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.0.0-RC1
Release Date | : February 17 2015