Class CoroutineScope
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineEnvironment
-
- de.esoco.coroutine.CoroutineScope
-
- All Implemented Interfaces:
org.obrel.core.Relatable
public class CoroutineScope extends CoroutineEnvironment
A scope that manages one or more running coroutines. A new scope is created through the factory methodlaunch(CoroutineContext, ScopeCode)It executes an instance of the functional interfaceCoroutineScope.ScopeCodeand 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 aCoroutineScope.ScopeFutureinstance. This sub-interface ofFuturecan 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 annotationMetaTypes.MANAGED.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCoroutineScope.ScopeCodeA functional interface that will be executed in a scope that has been launched withlaunch(ScopeCode).static classCoroutineScope.ScopeFuture<T>An implementation of the future interface that wraps a scope execution.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidawait()Blocks until all coroutines in this scope have finished execution.booleanawait(long timeout, java.util.concurrent.TimeUnit unit)Blocks until all coroutines in this scope have finished execution or a timeout expires.voidcancel()Cancels the execution of all coroutines that are currently running in this scope.CoroutineContextcontext()Returns the context in which coroutines of this scope are executed.<T> Channel<T>getChannel(ChannelId<T> id)Checks this scope and theCoroutineContextfor a channel with the given ID.longgetCoroutineCount()Returns the number of currently running coroutines.booleanhasChannel(ChannelId<?> id)Checks whether a channel with the given ID exists in this scope or in theCoroutineContext.booleanisCancelled()Checks whether this scope has been cancelled.booleanisCancelOnError()Checks whether the execution of the other coroutines in this scope is canceled if an exception occurs in a coroutine.booleanisFinished()Non-blockingly checks whether this scope has finished execution of all coroutines.static voidlaunch(CoroutineContext context, CoroutineScope.ScopeCode code)Launches a new scope for the execution of coroutine in a specific context.static voidlaunch(CoroutineScope.ScopeCode code)Launches a new scope for the execution of coroutine in thedefault context.static <T> CoroutineScope.ScopeFuture<T>produce(CoroutineContext context, java.util.function.Function<? super CoroutineScope,T> getResult, CoroutineScope.ScopeCode code)Launches a new scope that is expected to produce a result and returns aFutureinstance that can be used to query the result.static <T> CoroutineScope.ScopeFuture<T>produce(java.util.function.Function<? super CoroutineScope,T> getResult, CoroutineScope.ScopeCode code)Launches a new scope that is expected to produce a result in thedefault context.voidremoveChannel(ChannelId<?> id)Removes a channel from this scope or from theCoroutineContext.voidsetCancelOnError(boolean cancelOnError)Sets the behavior on coroutine errors in the scope.java.lang.StringtoString()-
Methods inherited from class de.esoco.coroutine.CoroutineEnvironment
createChannel
-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
-
-
-
-
Method Detail
-
launch
public static void launch(CoroutineContext context, CoroutineScope.ScopeCode code)
Launches a new scope for the execution of coroutine in a specific context. This method will block the invoking thread until all coroutines launched by the argument builder have terminated, either successfully, by cancelation, or with errors.If one or more of the coroutines or the scope code throw an exception this method will throw a
CoroutineScopeExceptionas soon as all other coroutines have terminated. By default an error causes all other coroutines to be cancelled but that can be changed withsetCancelOnError(boolean). If any other coroutines fail after the first error their continuations will also be added to the exception.- Parameters:
context- The coroutine context for the scopecode- The code to execute in the scope- Throws:
CoroutineScopeException- If one or more of the executed coroutines failed
-
launch
public static void launch(CoroutineScope.ScopeCode code)
Launches a new scope for the execution of coroutine in thedefault context.- Parameters:
code- The code to execute in the scope- See Also:
launch(CoroutineContext, ScopeCode)
-
produce
public static <T> CoroutineScope.ScopeFuture<T> produce(CoroutineContext context, java.util.function.Function<? super CoroutineScope,T> getResult, CoroutineScope.ScopeCode code)
Launches a new scope that is expected to produce a result and returns aFutureinstance that can be used to query the result. The result will be retrieved after the coroutine execution finished from the scope by applying the result function. If the future object is only needed to wrap a scope execution this function- Parameters:
context- The coroutine context for the scopegetResult- A function that retrieves the result from the scope or NULL to always return NULLcode- The producing code to execute in the scope- Returns:
- A future that provides access to the result of the scope execution
-
produce
public static <T> CoroutineScope.ScopeFuture<T> produce(java.util.function.Function<? super CoroutineScope,T> getResult, CoroutineScope.ScopeCode code)
Launches a new scope that is expected to produce a result in thedefault context.- Parameters:
getResult- A function that retrieves the result from the scope or NULL to always return NULLcode- The producing code to execute in the scope- See Also:
produce(CoroutineContext, Function, ScopeCode)
-
await
public void await()
Blocks until all coroutines in this scope have finished execution. If no coroutines are running or all have finished execution already this method returns immediately.
-
await
public boolean await(long timeout, java.util.concurrent.TimeUnit unit)Blocks until all coroutines in this scope have finished execution or a timeout expires. If the timeout is reached this method will return but the scope will continue to execute. If necessary it can be cancelled by callingcancel().- Parameters:
timeout- The maximum time to waitunit- The unit of the timeout- Returns:
- TRUE if the scope has finished execution; FALSE if the timeout was reached
-
cancel
public void cancel()
Cancels the execution of all coroutines that are currently running in this scope.
-
context
public CoroutineContext context()
Returns the context in which coroutines of this scope are executed.- Returns:
- The coroutine context
-
getChannel
public <T> Channel<T> getChannel(ChannelId<T> id)
Checks this scope and theCoroutineContextfor 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 withCoroutineEnvironment.createChannel(ChannelId, int).- Overrides:
getChannelin classCoroutineEnvironment- Parameters:
id- The channel ID- Returns:
- The channel for the given ID
- See Also:
CoroutineEnvironment.getChannel(ChannelId)
-
getCoroutineCount
public long getCoroutineCount()
Returns the number of currently running coroutines. This will only be a momentary value as the execution of the coroutines happens asynchronously and coroutines may finish while querying this count.- Returns:
- The number of running coroutines
-
hasChannel
public boolean hasChannel(ChannelId<?> id)
Checks whether a channel with the given ID exists in this scope or in theCoroutineContext.- Overrides:
hasChannelin classCoroutineEnvironment- Parameters:
id- The channel ID- Returns:
- TRUE if the channel exists in this context
- See Also:
CoroutineEnvironment.hasChannel(ChannelId)
-
isCancelOnError
public boolean isCancelOnError()
Checks whether the execution of the other coroutines in this scope is canceled if an exception occurs in a coroutine. Can be changed withsetCancelOnError(boolean).- Returns:
- TRUE if all coroutines are cancelled if a coroutine fails
-
isCancelled
public boolean isCancelled()
Checks whether this scope has been cancelled.- Returns:
- TRUE if cancelled
-
isFinished
public boolean isFinished()
Non-blockingly checks whether this scope has finished execution of all coroutines. Due to the asynchronous nature of coroutine executions this method will only return when preceded by a blocking call likeawait().- Returns:
- TRUE if finished
-
removeChannel
public void removeChannel(ChannelId<?> id)
Removes a channel from this scope or from theCoroutineContext. If it exists in both it will only be removed from this scope.- Overrides:
removeChannelin classCoroutineEnvironment- Parameters:
id- The channel ID- See Also:
CoroutineEnvironment.removeChannel(ChannelId)
-
setCancelOnError
public void setCancelOnError(boolean cancelOnError)
Sets the behavior on coroutine errors in the scope. If set to TRUE (which is the default) any exception in a coroutine will cancel the execution of this scope. If FALSE all other coroutines are allowed to finish execution (or fail too) before the scope's execution is finished. In any case the scope will throw aCoroutineScopeExceptionif one or more errors occurred.- Parameters:
cancelOnError- TRUE to cancel running coroutine if an error occurs; FALSE to let them finish
-
toString
public java.lang.String toString()
- Overrides:
toStringin classorg.obrel.core.RelatedObject
-
-