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 above 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 atomically records whether the callback for an asynchronous sub-task has been called during the processing of the asynchronous sub-task, and if so then the processing of the large asynchronous task iterates rather than recursing.
Subclasses must implement method process() where the
asynchronous sub-task is initiated and a suitable Action
is returned to this callback to indicate the overall progress of
the large asynchronous task.
This callback is passed to the asynchronous sub-task, and a call
to succeeded() on this callback represents the successful
completion of the asynchronous sub-task, while a call to
failed(Throwable) on this callback represents the
completion with a failure of the large asynchronous task.
| Modifier and Type | Class and Description |
|---|---|
| protected static enum | IteratingCallback.
The indication of the overall progress of the iteration
that implementations of |
| private static enum | IteratingCallback.
The internal states of this callback. |
| Modifier and Type | Field and Description |
|---|---|
| private Throwable | |
| private boolean | |
| private final AutoLock | |
| private IteratingCallback. |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| public void | failed(Throwable
the reason for the operation failure x)Overrides default org. Method to invoke when the asynchronous sub-task fails, or to fail the overall asynchronous task and therefore terminate the iteration. |
| public boolean | |
| public boolean | |
| public boolean | |
| pack-priv boolean | |
| public boolean | |
| public void | iterate()
This method must be invoked by applications to start the processing of asynchronous sub-tasks. |
| protected void | onCompleteFailure(Throwable
the throwable to indicate cause of failure cause)Invoked when the overall task has completed with a failure. |
| protected void | |
| protected abstract IteratingCallback. | |
| private void | |
| public boolean | |
| public void | succeeded()
Overrides default org. Method to invoke when the asynchronous sub-task succeeds. |
| public String |
| _failure | back to summary |
|---|---|
| private Throwable _failure | |
| _iterate | back to summary |
|---|---|
| private boolean _iterate | |
| _lock | back to summary |
|---|---|
| private final AutoLock _lock | |
| _state | back to summary |
|---|---|
| private IteratingCallback. | |
| IteratingCallback | back to summary |
|---|---|
| protected IteratingCallback() | |
| IteratingCallback | back to summary |
|---|---|
| protected IteratingCallback(boolean needReset) | |
| abort | back to summary |
|---|---|
| public void abort(Throwable failure) Method to invoke to stop further processing iterations. This method causes
| |
| close | back to summary |
|---|---|
| public void close() Method to invoke to forbid further invocations to
When this method is invoked during processing, it behaves like invoking
| |
| failed | back to summary |
|---|---|
| public void failed(Throwable x) Overrides default org. Method to invoke when the asynchronous sub-task fails, or to fail the overall asynchronous task and therefore terminate the iteration.
Subclasses that override this method must always remember
to call
Eventually,
| |
| isAborted | back to summary |
|---|---|
public boolean isAborted()
| |
| isClosed | back to summary |
|---|---|
public boolean isClosed()
| |
| isFailed | back to summary |
|---|---|
public boolean isFailed()
| |
| isIdle | back to summary |
|---|---|
pack-priv boolean isIdle()
| |
| isSucceeded | back to summary |
|---|---|
public boolean isSucceeded()
| |
| iterate | back to summary |
|---|---|
| public void iterate() This method must be invoked by applications to start the processing of asynchronous sub-tasks.
It can be called at any time by any thread, and its contract is that
when called, then the | |
| onCompleteFailure | back to summary |
|---|---|
| protected void onCompleteFailure(Throwable cause) Invoked when the overall task has completed with a failure.
| |
| onCompleteSuccess | back to summary |
|---|---|
| protected void onCompleteSuccess() Invoked when the overall task has completed successfully.
| |
| process | back to summary |
|---|---|
| protected abstract IteratingCallback. Method called by Implementations must initiate the asynchronous execution of the sub-task (if any) and return an appropriate action:
| |
| processing | back to summary |
|---|---|
| private void processing() | |
| reset | back to summary |
|---|---|
| public boolean reset() Resets this callback.
A callback can only be reset to the idle state from the
| |
| succeeded | back to summary |
|---|---|
| public void succeeded() Overrides default org. Method to invoke when the asynchronous sub-task succeeds.
Subclasses that override this method must always remember
to call
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
process() must return.
| Modifier and Type | Field and Description |
|---|---|
| public static final IteratingCallback. | |
| public static final IteratingCallback. | |
| public static final IteratingCallback. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static IteratingCallback. | |
| public static IteratingCallback. |
| IDLE | back to summary |
|---|---|
| public static final IteratingCallback. Indicates that | |
| SCHEDULED | back to summary |
|---|---|
| public static final IteratingCallback. Indicates that | |
| SUCCEEDED | back to summary |
|---|---|
| public static final IteratingCallback. Indicates that | |
| Action | back to summary |
|---|---|
| private Action() | |
| valueOf | back to summary |
|---|---|
| public static IteratingCallback. | |
| values | back to summary |
|---|---|
| public static IteratingCallback. | |
| Modifier and Type | Field and Description |
|---|---|
| public static final IteratingCallback. | |
| public static final IteratingCallback. | CALLED
The asynchronous sub-task was completed successfully
via a call to |
| public static final IteratingCallback. | |
| public static final IteratingCallback. | FAILED
The iteration terminated with a failure via a call
to |
| public static final IteratingCallback. | IDLE
This callback is idle, ready to iterate. |
| public static final IteratingCallback. | PENDING
Method |
| public static final IteratingCallback. | PROCESSING
This callback is just about to call |
| public static final IteratingCallback. | SUCCEEDED
The iteration terminated successfully as indicated by
|
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static IteratingCallback. | |
| public static IteratingCallback. |
| ABORTED | back to summary |
|---|---|
| public static final IteratingCallback. | |
| CALLED | back to summary |
|---|---|
| public static final IteratingCallback. The asynchronous sub-task was completed successfully
via a call to | |
| CLOSED | back to summary |
|---|---|
| public static final IteratingCallback. | |
| FAILED | back to summary |
|---|---|
| public static final IteratingCallback. The iteration terminated with a failure via a call
to | |
| IDLE | back to summary |
|---|---|
| public static final IteratingCallback. This callback is idle, ready to iterate. | |
| PENDING | back to summary |
|---|---|
| public static final IteratingCallback. Method | |
| PROCESSING | back to summary |
|---|---|
| public static final IteratingCallback. This callback is just about to call | |
| SUCCEEDED | back to summary |
|---|---|
| public static final IteratingCallback. The iteration terminated successfully as indicated by
| |
| State | back to summary |
|---|---|
| private State() | |
| valueOf | back to summary |
|---|---|
| public static IteratingCallback. | |
| values | back to summary |
|---|---|
| public static IteratingCallback. | |