public abstract class IteratingCallback extends Object implements Callback
A typical example is the write of a large content to a socket, divided in chunks. Chunk C1 is written by thread T1, which also invokes the callback, which writes chunk C2, which invokes the callback again, which writes chunk C3, and so forth.
The problem with the example is that if the callback thread is the same that performs the I/O operation, then the process is recursive and may result in a stack overflow. To avoid the stack overflow, a thread dispatch must be performed, causing context switching and cache misses, affecting performance.
To avoid this issue, this callback uses an AtomicReference to record whether success callback has been called during the processing of a sub task, and if so then the processing iterates rather than recurring.
Subclasses must implement method process() where the sub task is
executed and a suitable IteratingCallback.Action is returned to this
callback to indicate the overall progress of the job. This callback is passed
to the asynchronous execution of each sub task and a call the
succeeded() on this callback represents the completion of the sub
task.
| 限定符和类型 | 类和说明 |
|---|---|
protected static class |
IteratingCallback.Action
The indication of the overall progress of the overall job that
implementations of
process() must return. |
Callback.Completable, Callback.Nested, Callback.NonBlocking| 限定符 | 构造器和说明 |
|---|---|
protected |
IteratingCallback() |
protected |
IteratingCallback(boolean needReset) |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
close() |
void |
failed(Throwable x)
Invoked when the sub task fails.
|
boolean |
isClosed() |
boolean |
isFailed() |
boolean |
isSucceeded() |
void |
iterate()
This method must be invoked by applications to start the processing of
sub tasks.
|
protected void |
onCompleteFailure(Throwable cause)
Invoked when the overall task has completed with a failure.
|
protected void |
onCompleteSuccess()
Invoked when the overall task has completed successfully.
|
protected abstract IteratingCallback.Action |
process()
Method called by
iterate() to process the sub task. |
boolean |
reset()
Resets this callback.
|
void |
succeeded()
Invoked when the sub task succeeds.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitfrom, from, isNonBlockingprotected IteratingCallback()
protected IteratingCallback(boolean needReset)
protected abstract IteratingCallback.Action process() throws Exception
iterate() to process the sub task.
Implementations must start the asynchronous execution of the sub task (if any) and return an appropriate action:
IteratingCallback.Action.IDLE when no sub tasks are available for execution but
the overall job is not completed yetIteratingCallback.Action.SCHEDULED when the sub task asynchronous execution has
been startedIteratingCallback.Action.SUCCEEDED when the overall job is completedException - if the sub task processing throwsprotected void onCompleteSuccess()
protected void onCompleteFailure(Throwable cause)
cause - the throwable to indicate cause of failureonCompleteSuccess()public void iterate()
process() method will be called
during or soon after, either by the calling thread or by another thread.public void succeeded()
super.succeeded().succeeded 在接口中 CallbackCallback.failed(Throwable)public void failed(Throwable x)
super.failed(Throwable).public void close()
public boolean isClosed()
public boolean isFailed()
public boolean isSucceeded()
public boolean reset()
A callback can only be reset to IDLE from the SUCCEEDED or FAILED states or if it is already IDLE.
Copyright © 2016. All rights reserved.