public class CoroutineScope extends CoroutineEnvironment
launch(CoroutineContext, ScopeCode)
It
executes an instance of the functional interface CoroutineScope.ScopeCode
and blocks
the invoking thread until the code and all coroutines by it have finished
execution (either successfully or with an exception).
An alternative way to launch a scope is by using the method produce(CoroutineContext, Function, ScopeCode)
. It also executes the given
code (which in turn may start coroutines) but returns immediately after the
code finished with a CoroutineScope.ScopeFuture
instance. This sub-interface of
Future
can then be used to wait for the started coroutines to finish
or to cancel the execution. As the name indicates, this method is mainly
intended for scope executions that produce result. But it can also be used to
just wrap a scope execution to handle it as a future and then ignore the
result.
A scope will also automatically close all (AutoCloseable
)
resources that are stored in it with relations that have the annotation
MetaTypes.MANAGED
.
Modifier and Type | Class and Description |
---|---|
static interface |
CoroutineScope.ScopeCode
A functional interface that will be executed in a scope that has been
launched with
launch(ScopeCode) . |
static class |
CoroutineScope.ScopeFuture<T>
An implementation of the future interface that wraps a scope execution.
|
Modifier and Type | Method and Description |
---|---|
void |
await()
Blocks until all coroutines in this scope have finished execution.
|
boolean |
await(long nTimeout,
java.util.concurrent.TimeUnit eUnit)
Blocks until all coroutines in this scope have finished execution or a
timeout expires.
|
void |
cancel()
Cancels the execution of all coroutines that are currently running in
this scope.
|
CoroutineContext |
context()
Returns the context in which coroutines of this scope are executed.
|
<T> Channel<T> |
getChannel(ChannelId<T> rId)
Checks this scope and the
CoroutineContext for a channel with the
given ID. |
long |
getCoroutineCount()
Returns the number of currently running coroutines.
|
boolean |
hasChannel(ChannelId<?> rId)
Checks whether a channel with the given ID exists in this scope or in the
CoroutineContext . |
boolean |
isCancelled()
Checks whether this scope has been cancelled.
|
boolean |
isCancelOnError()
Checks whether the execution of the other coroutines in this scope is
canceled if an exception occurs in a coroutine.
|
boolean |
isFinished()
Non-blockingly checks whether this scope has finished execution of all
coroutines.
|
static void |
launch(CoroutineContext rContext,
CoroutineScope.ScopeCode rCode)
Launches a new scope for the execution of coroutine in a specific
context.
|
static void |
launch(CoroutineScope.ScopeCode rCode)
Launches a new scope for the execution of coroutine in the
default context . |
static <T> CoroutineScope.ScopeFuture<T> |
produce(CoroutineContext rContext,
java.util.function.Function<? super CoroutineScope,T> fGetResult,
CoroutineScope.ScopeCode rCode)
Launches a new scope that is expected to produce a result and returns a
Future instance that can be used to query the result. |
static <T> CoroutineScope.ScopeFuture<T> |
produce(java.util.function.Function<? super CoroutineScope,T> fGetResult,
CoroutineScope.ScopeCode rCode)
Launches a new scope that is expected to produce a result in the
default context . |
void |
removeChannel(ChannelId<?> rId)
Removes a channel from this scope or from the
CoroutineContext . |
void |
setCancelOnError(boolean bCancel)
Sets the behavior on coroutine errors in the scope.
|
java.lang.String |
toString() |
createChannel
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
public static void launch(CoroutineScope.ScopeCode rCode)
default context
.rCode
- The code to execute in the scopelaunch(CoroutineContext, ScopeCode)
public static void launch(CoroutineContext rContext, CoroutineScope.ScopeCode rCode)
If one or more of the coroutines or the scope code throw an exception
this method will throw a CoroutineScopeException
as soon as all
other coroutines have terminated. By default an error causes all other
coroutines to be cancelled but that can be changed with setCancelOnError(boolean)
. If any other coroutines fail after the first
error their continuations will also be added to the exception.
rContext
- The coroutine context for the scoperCode
- The code to execute in the scopeCoroutineScopeException
- If one or more of the executed coroutines
failedpublic static <T> CoroutineScope.ScopeFuture<T> produce(java.util.function.Function<? super CoroutineScope,T> fGetResult, CoroutineScope.ScopeCode rCode)
default context
.fGetResult
- A function that retrieves the result from the scope or
NULL to always return NULLrCode
- The producing code to execute in the scope#produce(CoroutineContext, RelationType, ScopeCode)
public static <T> CoroutineScope.ScopeFuture<T> produce(CoroutineContext rContext, java.util.function.Function<? super CoroutineScope,T> fGetResult, CoroutineScope.ScopeCode rCode)
Future
instance that can be used to query the result. The result
must be stored by the scope code in a relation of the scope with the
given relation type. If only a future object is needed to manage a scope
execution the relation type can be NULL, in which case the future result
will also be NULL.rContext
- The coroutine context for the scopefGetResult
- A function that retrieves the result from the scope or
NULL to always return NULLrCode
- The producing code to execute in the scopepublic void await()
public boolean await(long nTimeout, java.util.concurrent.TimeUnit eUnit)
cancel()
.nTimeout
- The maximum time to waiteUnit
- The unit of the timeoutpublic void cancel()
public CoroutineContext context()
public <T> Channel<T> getChannel(ChannelId<T> rId)
CoroutineContext
for a channel with the
given ID. If no such channel exists it will be created in this scope. If
a context channel is needed instead it needs to be created in advance
with CoroutineEnvironment.createChannel(ChannelId, int)
.getChannel
in class CoroutineEnvironment
rId
- The channel IDCoroutineEnvironment.getChannel(ChannelId)
public long getCoroutineCount()
public boolean hasChannel(ChannelId<?> rId)
CoroutineContext
.hasChannel
in class CoroutineEnvironment
rId
- The channel IDCoroutineEnvironment.hasChannel(ChannelId)
public boolean isCancelled()
public boolean isCancelOnError()
setCancelOnError(boolean)
.public boolean isFinished()
await()
.public void removeChannel(ChannelId<?> rId)
CoroutineContext
.
If it exists in both it will only be removed from this scope.removeChannel
in class CoroutineEnvironment
rId
- The channel IDCoroutineEnvironment.removeChannel(ChannelId)
public void setCancelOnError(boolean bCancel)
CoroutineScopeException
if one or more errors
occurred.bCancel
- TRUE to cancel running coroutine if an error occurs; FALSE
to let them finishpublic java.lang.String toString()
toString
in class org.obrel.core.RelatedObject