Package de.esoco.coroutine
Class CoroutineContext
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineEnvironment
-
- de.esoco.coroutine.CoroutineContext
-
- All Implemented Interfaces:
org.obrel.core.Relatable
public class CoroutineContext extends CoroutineEnvironment
The context for the execution ofCoroutines.
-
-
Constructor Summary
Constructors Constructor Description CoroutineContext()Creates a new instance that uses thecommon thread poolas the executor.CoroutineContext(java.util.concurrent.Executor executor)Creates a new instance with a specific coroutine executor.CoroutineContext(java.util.concurrent.Executor executor, java.util.concurrent.ScheduledExecutorService scheduler)Creates a new instance with a specific coroutine executor and scheduler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidawaitAllScopes()Blocks until the coroutines of allscopesin this context have finished execution.java.util.concurrent.ExecutorgetExecutor()Returns the executor to be used for the execution of the steps of aCoroutine.java.util.concurrent.ScheduledExecutorServicegetScheduler()Returns the executor to be used for the execution of timed steps in aCoroutine.longgetScopeCount()Returns the number of currently activescopes.-
Methods inherited from class de.esoco.coroutine.CoroutineEnvironment
createChannel, getChannel, hasChannel, removeChannel
-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, toString, transform, writeRelations
-
-
-
-
Constructor Detail
-
CoroutineContext
public CoroutineContext()
Creates a new instance that uses thecommon thread poolas the executor.
-
CoroutineContext
public CoroutineContext(java.util.concurrent.Executor executor)
Creates a new instance with a specific coroutine executor. If the executor also implements theScheduledExecutorServiceinterface it will also be used for scheduling purposes.- Parameters:
executor- The coroutine executor
-
CoroutineContext
public CoroutineContext(java.util.concurrent.Executor executor, java.util.concurrent.ScheduledExecutorService scheduler)Creates a new instance with a specific coroutine executor and scheduler. The latter will be used to execute timed coroutine steps.- Parameters:
executor- The coroutine executorscheduler- The scheduled executor service
-
-
Method Detail
-
awaitAllScopes
public void awaitAllScopes()
Blocks until the coroutines of allscopesin this context have finished execution. If no coroutines are running or all have finished execution already this method returns immediately.
-
getExecutor
public java.util.concurrent.Executor getExecutor()
Returns the executor to be used for the execution of the steps of aCoroutine.- Returns:
- The coroutine executor for this context
-
getScheduler
public java.util.concurrent.ScheduledExecutorService getScheduler()
Returns the executor to be used for the execution of timed steps in aCoroutine. If no scheduler has been set in the constructor or created before a new instance with a pool size of 1 will be created by invokingExecutors.newScheduledThreadPool(int).- Returns:
- The coroutine scheduler for this context
-
getScopeCount
public long getScopeCount()
Returns the number of currently activescopes. This will only be a momentary value as the execution of the coroutines in the scopes happens asynchronously and some coroutines may finish while querying this count.- Returns:
- The number of running coroutines
-
-