de.matthiasmann.continuations
Class Coroutine

java.lang.Object
  extended by de.matthiasmann.continuations.Coroutine
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable

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.

See Also:
Serialized Form

Nested Class Summary
static class Coroutine.State
           
 
Constructor Summary
Coroutine(CoroutineProto proto)
          Creates a new Coroutine from the given CoroutineProto.
 
Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Coroutine

public Coroutine(CoroutineProto proto)
Creates a new Coroutine from the given CoroutineProto. A CoroutineProto can be used in several Coroutines at the same time - but then the normal multi threading rules apply to the member state.

Parameters:
proto - The CoroutineProto for the Coroutine.
Method Detail

yield

public static void yield()
                  throws SuspendExecution,
                         java.lang.IllegalStateException
Suspend the currently running Coroutine on the calling thread.

Throws:
SuspendExecution - This exception is used for control transfer - don't catch it !
java.lang.IllegalStateException - If not called from a Coroutine

getActiveCoroutine

public static Coroutine getActiveCoroutine()
Returns the active Coroutine on this thread or NULL if no coroutine is running.

Returns:
the active Coroutine on this thread or NULL if no coroutine is running.

getProto

public CoroutineProto getProto()
Returns the CoroutineProto that is used for this Coroutine

Returns:
The CoroutineProto that is used for this Coroutine

getState

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.

Returns:
The current state of this Coroutine
See Also:
run()

run

public void run()
         throws java.lang.IllegalStateException
Runs the Coroutine until it is finished or suspended. This method must only be called when the Coroutine is in the states NEW or SUSPENDED. It is not multi threading safe.

Specified by:
run in interface java.lang.Runnable
Throws:
java.lang.IllegalStateException - if the Coroutine is currently running or already finished.