Top Description Inners Fields Constructors Methods
org.glassfish.grizzly.impl

public Class SafeFutureImpl<R>

extends Object
implements FutureImpl<R>
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.impl.FutureImpl, org.glassfish.grizzly.GrizzlyFuture, org.glassfish.grizzly.Cacheable, java.util.concurrent.Future
Annotations
@SuppressWarnings:deprecation
Imports
java.util.HashSet, .Iterator, .Set, java.util.concurrent.CancellationException, .ExecutionException, .Future, .TimeUnit, .TimeoutException, java.util.concurrent.locks.AbstractQueuedSynchronizer, org.glassfish.grizzly.CompletionHandler

Safe FutureImpl implementation.

(Based on the JDK java.util.concurrent.FutureTask)

See Also
Future

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
SafeFutureImpl.Sync

Synchronization control for FutureTask.

Field Summary

Modifier and TypeField and Description
private final Object
private Set<CompletionHandler<R>>
private final SafeFutureImpl<R>.Sync
sync

Synchronization control for FutureTask

Constructor Summary

AccessConstructor and Description
public
SafeFutureImpl()

Creates SafeFutureImpl

Method Summary

Modifier and TypeMethod and Description
public void
addCompletionHandler(final CompletionHandler<R> completionHandler)

Implements org.glassfish.grizzly.GrizzlyFuture.addCompletionHandler.

Adds a CompletionHandler, which will be notified once the asynchronous computation, represented by this Future, is complete.

public boolean
cancel(boolean mayInterruptIfRunning)

Implements java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task.

public static <R> SafeFutureImpl<R>
create()

Construct SafeFutureImpl.

protected void
done()

Protected method invoked when this task transitions to state isDone (whether normally or via cancellation).

public void
failure(Throwable failure)

Implements org.glassfish.grizzly.impl.FutureImpl.failure.

Notify about the failure, occurred during asynchronous operation execution.

public R
get()

Implements java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result.

public R
get(long timeout, TimeUnit unit)

Implements java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

public R
getResult()

Implements org.glassfish.grizzly.impl.FutureImpl.getResult.

Get current result value without any blocking.

public boolean
isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally.

public boolean
isDone()

Implements java.util.concurrent.Future.isDone.

Returns true if this task completed.

public void
markForRecycle(boolean recycleResult)

Implements org.glassfish.grizzly.GrizzlyFuture.markForRecycle.

Mark GrizzlyFuture as recyclable, so once result will come - GrizzlyFuture object will be recycled and returned to a thread local object pool.

private void
notifyCompletionHandler(final CompletionHandler<R> completionHandler)

Notify single CompletionHandler about the result.

private void
notifyCompletionHandlers()

Notify registered CompletionHandlers about the result.

protected void
onComplete()

The method is called when this SafeFutureImpl is marked as completed.

public void
recycle(boolean recycleResult)

Implements org.glassfish.grizzly.GrizzlyFuture.recycle.

Recycle GrizzlyFuture now.

public void
public void
result(R
the result value
result
)

Implements org.glassfish.grizzly.impl.FutureImpl.result.

Set the result value and notify about operation completion.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

chSyncback to summary
private final Object chSync
completionHandlersback to summary
private Set<CompletionHandler<R>> completionHandlers
syncback to summary
private final SafeFutureImpl<R>.Sync sync

Synchronization control for FutureTask

Constructor Detail

SafeFutureImplback to summary
public SafeFutureImpl()

Creates SafeFutureImpl

Annotations
@SuppressWarnings:unchecked

Method Detail

addCompletionHandlerback to summary
public void addCompletionHandler(final CompletionHandler<R> completionHandler)

Implements org.glassfish.grizzly.GrizzlyFuture.addCompletionHandler.

Doc from org.glassfish.grizzly.GrizzlyFuture.addCompletionHandler.

Adds a CompletionHandler, which will be notified once the asynchronous computation, represented by this Future, is complete.

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

Implements java.util.concurrent.Future.cancel.

Doc from java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task. This method has no effect if the task is already completed or cancelled, or could not be cancelled for some other reason. Otherwise, if this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task (when known by the implementation) is interrupted in an attempt to stop the task.

The return value from this method does not necessarily indicate whether the task is now cancelled; use isCancelled.

Annotations
@Override
createback to summary
public static <R> SafeFutureImpl<R> create()

Construct SafeFutureImpl.

Annotations
@SuppressWarnings:unchecked
doneback to summary
protected void done()

Protected method invoked when this task transitions to state isDone (whether normally or via cancellation). The default implementation does nothing. Subclasses may override this method to invoke completion callbacks or perform bookkeeping. Note that you can query status inside the implementation of this method to determine whether this task has been cancelled.

failureback to summary
public void failure(Throwable failure)

Implements org.glassfish.grizzly.impl.FutureImpl.failure.

Notify about the failure, occurred during asynchronous operation execution.

Annotations
@Override
getback to summary
public R get() throws InterruptedException, ExecutionException

Implements java.util.concurrent.Future.get.

Doc from java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread was interrupted while waiting
ExecutionException:
if the computation threw an exception
CancellationException:
if the computation was cancelled
getback to summary
public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException

Implements java.util.concurrent.Future.get.

Doc from java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread was interrupted while waiting
ExecutionException:
if the computation threw an exception
TimeoutException:
if the wait timed out
CancellationException:
if the computation was cancelled
getResultback to summary
public R getResult()

Implements org.glassfish.grizzly.impl.FutureImpl.getResult.

Doc from org.glassfish.grizzly.impl.FutureImpl.getResult.

Get current result value without any blocking.

Annotations
@Override
isCancelledback to summary
public boolean isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Doc from java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally.

Annotations
@Override
isDoneback to summary
public boolean isDone()

Implements java.util.concurrent.Future.isDone.

Doc from java.util.concurrent.Future.isDone.

Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

Annotations
@Override
markForRecycleback to summary
public void markForRecycle(boolean recycleResult)

Implements org.glassfish.grizzly.GrizzlyFuture.markForRecycle.

Doc from org.glassfish.grizzly.GrizzlyFuture.markForRecycle.

Mark GrizzlyFuture as recyclable, so once result will come - GrizzlyFuture object will be recycled and returned to a thread local object pool. You can consider to use this method, if you're not interested in using this GrizzlyFuture object.

Annotations
@Override
notifyCompletionHandlerback to summary
private void notifyCompletionHandler(final CompletionHandler<R> completionHandler)

Notify single CompletionHandler about the result.

notifyCompletionHandlersback to summary
private void notifyCompletionHandlers()

Notify registered CompletionHandlers about the result.

onCompleteback to summary
protected void onComplete()

The method is called when this SafeFutureImpl is marked as completed. Subclasses can override this method.

recycleback to summary
public void recycle(boolean recycleResult)

Implements org.glassfish.grizzly.GrizzlyFuture.recycle.

Doc from org.glassfish.grizzly.GrizzlyFuture.recycle.

Recycle GrizzlyFuture now. This method could be used, if you're not interested in using this GrizzlyFuture object, and you're sure this object is not used by any other application part.

Annotations
@Override
recycleback to summary
public void recycle()

Implements org.glassfish.grizzly.Cacheable.recycle.

Annotations
@Override
resultback to summary
public void result(R result)

Implements org.glassfish.grizzly.impl.FutureImpl.result.

Set the result value and notify about operation completion.

Parameters
result:R

the result value

Annotations
@Override
org.glassfish.grizzly.impl back to summary

private final Class SafeFutureImpl.Sync

extends AbstractQueuedSynchronizer
Class Inheritance

Synchronization control for FutureTask. Note that this must be a non-static inner class in order to invoke the protected done method. For clarity, all inner class support methods are same as outer, prefixed with "inner".

Uses AQS sync state to represent run status

Field Summary

Modifier and TypeField and Description
private static final int
CANCELLED

Hides java.util.concurrent.locks.AbstractQueuedSynchronizer.CANCELLED.

State value representing that task was cancelled
private Throwable
exception

The exception to throw from get()

private static final int
RAN

State value representing that task ran

private static final int
READY

State value representing that task is ready to run

private static final int
RESULT

State value representing that result/exception is being set

private R
result

The result to return from get()

private static final long

Constructor Summary

AccessConstructor and Description
private
Sync()

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
innerCancel(boolean mayInterruptIfRunning)

pack-priv R
pack-priv R
innerGet(long nanosTimeout)

pack-priv boolean
pack-priv void
innerSet(R v)

pack-priv void
private boolean
protected int
tryAcquireShared(int ignore)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Implements AQS base acquire to succeed if ran or cancelled

protected boolean
tryReleaseShared(int ignore)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Implements AQS base release to always signal after setting final done status by nulling runner thread.

Inherited from java.util.concurrent.locks.AbstractQueuedSynchronizer:
acquireacquireInterruptiblyacquireSharedacquireSharedInterruptiblycompareAndSetStategetExclusiveQueuedThreadsgetFirstQueuedThreadgetQueuedThreadsgetQueueLengthgetSharedQueuedThreadsgetStategetWaitingThreadsgetWaitQueueLengthhasContendedhasQueuedPredecessorshasQueuedThreadshasWaitersisHeldExclusivelyisQueuedownsreleasereleaseSharedsetStatetoStringtryAcquiretryAcquireNanostryAcquireSharedNanostryRelease

Field Detail

CANCELLEDback to summary
private static final int CANCELLED

Hides java.util.concurrent.locks.AbstractQueuedSynchronizer.CANCELLED.

State value representing that task was cancelled

exceptionback to summary
private Throwable exception

The exception to throw from get()

RANback to summary
private static final int RAN

State value representing that task ran

READYback to summary
private static final int READY

State value representing that task is ready to run

RESULTback to summary
private static final int RESULT

State value representing that result/exception is being set

resultback to summary
private R result

The result to return from get()

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.locks.AbstractQueuedSynchronizer.serialVersionUID.

Constructor Detail

Syncback to summary
private Sync()

Method Detail

innerCancelback to summary
pack-priv boolean innerCancel(boolean mayInterruptIfRunning)
innerGetback to summary
pack-priv R innerGet() throws InterruptedException, ExecutionException
innerGetback to summary
pack-priv R innerGet(long nanosTimeout) throws InterruptedException, ExecutionException, TimeoutException
innerIsCancelledback to summary
pack-priv boolean innerIsCancelled()
innerSetback to summary
pack-priv void innerSet(R v)
innerSetExceptionback to summary
pack-priv void innerSetException(Throwable t)
ranOrCancelledback to summary
private boolean ranOrCancelled()
tryAcquireSharedback to summary
protected int tryAcquireShared(int ignore)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireShared.

Implements AQS base acquire to succeed if ran or cancelled

Annotations
@Override
tryReleaseSharedback to summary
protected boolean tryReleaseShared(int ignore)

Overrides java.util.concurrent.locks.AbstractQueuedSynchronizer.tryReleaseShared.

Implements AQS base release to always signal after setting final done status by nulling runner thread.

Annotations
@Override