public interface AsyncEpoch
AsyncEpoch epoch = AsyncEpoch.newEpoch();
Resource r = ...
// returns true if write succeeded
boolean write(Data data) {
epoch.enter().map(token -> {
try {
r.write(data);
return true;
} finally {
token.close();
}
}).orElse(false);
}
// stage completes when all outstanding writes are finished
CompletionStage<Void> close() { return epoch.terminate(); }
Modifier and Type | Interface | Description |
---|---|---|
static interface |
AsyncEpoch.EpochToken |
A token signifying successful entry in the active epoch.
|
Modifier and Type | Method | Description |
---|---|---|
CompletionStage<Void> |
awaitCompletion() |
Waits for the epoch to complete, returning a stage that completes after the epoch has been
terminated and all participants have exited. |
Optional<AsyncEpoch.EpochToken> |
enter() |
Attempts to secure a position in the active epoch, failing if it has already been terminated.
|
boolean |
isTerminated() |
Returns
true if this epoch has been terminated. |
static AsyncEpoch |
newEpoch() |
|
static AsyncEpoch |
newTerminatedEpoch() |
|
CompletionStage<Boolean> |
terminate() |
Atomically ends the active epoch, preventing new entrants from successfully entering and
returning a
CompletionStage that triggers once all active participants have exited. |
Optional<AsyncEpoch.EpochToken> enter()
Optional
populated with an AsyncEpoch.EpochToken
associated with the active
epoch if it has not been terminated. Otherwise, returns an empty OptionalCompletionStage<Boolean> terminate()
CompletionStage
that triggers once all active participants have exited.CompletionStage
, which will complete after the last open AsyncEpoch.EpochToken
has been closed. The returned stage will complete with false
if this epoch was
already terminated, otherwise true
for the single call that terminates this
epochboolean isTerminated()
true
if this epoch has been terminated. This boolean does not indicate
whether all active participants have exited the epoch, only whether the terminate()
method has been called and subsequent entrants will be rejectedCompletionStage<Void> awaitCompletion()
terminated
and all participants have exited. Note that this method does
not terminate the epoch itself -- new entrants may enter and exit freely after this
method is called, and a separate call to terminate()
must be made before this returned
stage completesCompletionStage
which will complete after a call to terminate()
has
been made, and the last open AsyncEpoch.EpochToken
has been closed.static AsyncEpoch newEpoch()
AsyncEpoch
instancestatic AsyncEpoch newTerminatedEpoch()
AsyncEpoch
instance which is already terminated.Copyright © 2018. All rights reserved.