V - The value returned by the async requestE - An exception class that could be thrown by the async requestpublic abstract class FiberAsync<V,E extends java.lang.Throwable>
extends java.lang.Object
implements java.io.Serializable
Foo.asyncOp(FooCompletion callback) is an asynchronous operation, where Completion is defined as:
interface FooCompletion {
void success(String result);
void failure(FooException exception);
}
We then define the following subclass:
class FooAsync extends FiberAsync<String, FooException> implements FooCompletion {
{@literal @}Override
public void success(String result) {
asyncCompleted(result);
}
{@literal @}Override
public void failure(FooException exception) {
asyncFailed(exception);
}
}
Then, to turn the operation into a fiber-blocking one, we can define:
String op() {
return new FooAsync() {
protected void requestAsync() {
Foo.asyncOp(this);
}
}.run();
}
| Constructor and Description |
|---|
FiberAsync()
Same as `FiberAsync(false)`
|
FiberAsync(boolean immediateExec) |
| Modifier and Type | Method and Description |
|---|---|
protected void |
asyncCompleted(V result)
This method must be called by the callback upon successful completion of the asynchronous operation.
|
protected void |
asyncFailed(java.lang.Throwable t)
This method must be called by the callback upon a failure of the asynchronous operation.
|
V |
getResult()
Returns the result of the asynchronous operation if it has completed, or throws an exception if it completed unsuccessfully.
|
protected void |
interrupted()
Called when the fiber calling
run is interrupted during the call. |
boolean |
isCompleted()
Tests whether or not the asynchronous operation represented by this `FiberAsyc` has completed.
|
protected void |
prepark()
Called by the fiber if this `FiberAsync` is in immediate-exec mode, immediately before attempting to block while running
in the callback's thread.
|
protected abstract void |
requestAsync()
A user of this class must override this method to start the asynchronous operation and register the callback.
|
protected V |
requestSync()
Called if
run() is not being executed in a fiber. |
protected V |
requestSync(long timeout,
java.util.concurrent.TimeUnit unit)
Called if
run(long, TimeUnit) is not being executed in a fiber. |
V |
run()
Runs the asynchronous operation, blocks until it completes and returns its result.
|
V |
run(long timeout,
java.util.concurrent.TimeUnit unit)
Runs the asynchronous operation, blocks until it completes (but only up to the given timeout duration) and returns its result.
|
V |
run(Timeout timeout)
Runs the asynchronous operation, blocks until it completes (but only up to the given timeout duration) and returns its result.
|
static <V,E extends java.lang.Exception> |
runBlocking(java.util.concurrent.ExecutorService exec,
CheckedCallable<V,E> callable)
Runs a thread-blocking operation on a given thread pool, blocks (the fiber) until the operation completes and returns
its result.
|
static <V,E extends java.lang.Exception> |
runBlocking(java.util.concurrent.ExecutorService exec,
long timeout,
java.util.concurrent.TimeUnit unit,
CheckedCallable<V,E> callable)
Runs a thread-blocking operation on a given thread pool, blocks (the fiber) until the operation completes
(but no longer than the specified timeout) and returns its result.
|
static <V,E extends java.lang.Exception> |
runBlocking(java.util.concurrent.ExecutorService exec,
Timeout timeout,
CheckedCallable<V,E> callable)
Runs a thread-blocking operation on a given thread pool, blocks (the fiber) until the operation completes
(but no longer than the specified timeout) and returns its result.
|
protected void |
waitForRegistration()
Spins until
requestAsync returns. |
protected E |
wrapException(java.lang.Throwable t)
Takes the exception generated by the async operation and possibly wraps it in an exception
that will be thrown by the
run method. |
public FiberAsync()
public FiberAsync(boolean immediateExec)
immediateExec - Whether the fiber should be executed in the same thread as the callback. Should generally be set to `false`.public V run() throws E extends java.lang.Throwable, SuspendExecution, java.lang.InterruptedException
Fiber.yield()
to return from the handler.asyncCompleted.E - if the async computation failed and an exception was set in a call to asyncFailed.java.lang.InterruptedExceptionE extends java.lang.ThrowableSuspendExecutionpublic V run(long timeout, java.util.concurrent.TimeUnit unit) throws E extends java.lang.Throwable, SuspendExecution, java.lang.InterruptedException, java.util.concurrent.TimeoutException
Fiber.yield()
to return from the handler.timeout - the maximum duration to wait for the resultunit - timeout's time unitasyncCompleted.E - if the async computation failed and an exception was set in a call to asyncFailed.java.util.concurrent.TimeoutException - if the operation had not completed by the time the timeout has elapsed.java.lang.InterruptedExceptionE extends java.lang.ThrowableSuspendExecutionpublic V run(Timeout timeout) throws E extends java.lang.Throwable, SuspendExecution, java.lang.InterruptedException, java.util.concurrent.TimeoutException
Fiber.yield()
to return from the handler.timeout - the method will not block for longer than the amount remaining in the TimeoutasyncCompleted.E - if the async computation failed and an exception was set in a call to asyncFailed.java.util.concurrent.TimeoutException - if the operation had not completed by the time the timeout has elapsed.java.lang.InterruptedExceptionE extends java.lang.ThrowableSuspendExecutionprotected final void waitForRegistration()
requestAsync returns. Can be called from overrides of run (and must be called by the fiber that's
calling run).protected void interrupted()
run is interrupted during the call.protected abstract void requestAsync()
ThreadLocals.protected V requestSync() throws E extends java.lang.Throwable, java.lang.InterruptedException, java.util.concurrent.ExecutionException
run() is not being executed in a fiber. Should perform the operation synchronously and return its result.
The default implementation of this method throws an `IllegalThreadStateException`.Ejava.lang.InterruptedExceptionE extends java.lang.Throwablejava.util.concurrent.ExecutionExceptionprotected V requestSync(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException, E extends java.lang.Throwable
run(long, TimeUnit) is not being executed in a fiber. Should perform the operation synchronously and return its result.
The default implementation of this method throws an `IllegalThreadStateException`.timeout - the maximum duration to wait for the resultunit - timeout's time unitEjava.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutExceptionE extends java.lang.Throwableprotected void asyncCompleted(V result)
result - The operation's resultprotected void asyncFailed(java.lang.Throwable t)
t - The exception that caused the failure, or an exception to be associated with it. Must not be `null`.protected void prepark()
public final boolean isCompleted()
public final V getResult() throws E extends java.lang.Throwable
E - if the async computation failed and an exception was set in a call to asyncFailed.java.lang.IllegalStateException - if the operation has not yet completed.E extends java.lang.Throwableprotected E wrapException(java.lang.Throwable t)
run method.public static <V,E extends java.lang.Exception> V runBlocking(java.util.concurrent.ExecutorService exec,
CheckedCallable<V,E> callable)
throws E extends java.lang.Exception,
SuspendExecution,
java.lang.InterruptedException
exec - the thread-pool on which the thread-blocking operation will be runcallable - the operationE - if the operation has thrown an exceptionE extends java.lang.ExceptionSuspendExecutionjava.lang.InterruptedExceptionpublic static <V,E extends java.lang.Exception> V runBlocking(java.util.concurrent.ExecutorService exec,
long timeout,
java.util.concurrent.TimeUnit unit,
CheckedCallable<V,E> callable)
throws E extends java.lang.Exception,
SuspendExecution,
java.lang.InterruptedException,
java.util.concurrent.TimeoutException
exec - the thread-pool on which the thread-blocking operation will be runtimeout - the maximum duration to wait for the operation to completeunit - timeout's time unit.callable - the operationE - if the operation has thrown an exceptionjava.util.concurrent.TimeoutException - if the timeout expires before the operation completes.E extends java.lang.ExceptionSuspendExecutionjava.lang.InterruptedExceptionpublic static <V,E extends java.lang.Exception> V runBlocking(java.util.concurrent.ExecutorService exec,
Timeout timeout,
CheckedCallable<V,E> callable)
throws E extends java.lang.Exception,
SuspendExecution,
java.lang.InterruptedException,
java.util.concurrent.TimeoutException
exec - the thread-pool on which the thread-blocking operation will be runtimeout - the maximum duration to wait for the operation to completecallable - the operationE - if the operation has thrown an exceptionjava.util.concurrent.TimeoutException - if the timeout expires before the operation completes.E extends java.lang.ExceptionSuspendExecutionjava.lang.InterruptedException