public class Coroutine
extends java.lang.Object
implements java.lang.Runnable, java.io.Serializable
A Coroutine is used to run a CoroutineProto.
It also provides a function to suspend a running Coroutine.
A Coroutine can be serialized if it's not running and all involved
classes and data types are also Serializable
.
Modifier and Type | Class and Description |
---|---|
static class |
Coroutine.State |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_STACK_SIZE
Default stack size for the data stack.
|
Constructor and Description |
---|
Coroutine(CoroutineProto proto)
Creates a new Coroutine from the given CoroutineProto.
|
Coroutine(CoroutineProto proto,
int stackSize)
Creates a new Coroutine from the given CoroutineProto.
|
Modifier and Type | Method and Description |
---|---|
static Coroutine |
getActiveCoroutine()
Returns the active Coroutine on this thread or NULL if no coroutine is running.
|
CoroutineProto |
getProto()
Returns the CoroutineProto that is used for this Coroutine
|
Coroutine.State |
getState()
Returns the current state of this Coroutine.
|
void |
run()
Runs the Coroutine until it is finished or suspended.
|
static void |
yield()
Suspend the currently running Coroutine on the calling thread.
|
public static final int DEFAULT_STACK_SIZE
public Coroutine(CoroutineProto proto)
proto
- the CoroutineProto for the Coroutine.public Coroutine(CoroutineProto proto, int stackSize)
proto
- the CoroutineProto for the Coroutine.stackSize
- the initial stack size for the data stackjava.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0public static void yield() throws SuspendExecution, java.lang.IllegalStateException
SuspendExecution
- This exception is used for control transfer - don't catch it !java.lang.IllegalStateException
- If not called from a Coroutinepublic static Coroutine getActiveCoroutine()
public CoroutineProto getProto()
public Coroutine.State getState()
Returns the current state of this Coroutine. May be called by the Coroutine itself but should not be called by another thread.
The Coroutine starts in the state NEW then changes to RUNNING. From RUNNING it may change to FINISHED or SUSPENDED. SUSPENDED can only change to RUNNING by calling run() again.
run()
public void run() throws java.lang.IllegalStateException
run
in interface java.lang.Runnable
java.lang.IllegalStateException
- if the Coroutine is currently running or already finished.