|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.javaflow.Continuation
public final class Continuation
Snapshot of a thread execution state.
A Continuation
object is an immutable object that captures everything in
the Java stack. This includes
(1) current instruction pointer,
(2) return addresses, and
(3) local variables.
Continuation objects are used to restore the captured execution states later.
Method Summary | |
---|---|
static void |
again()
Jumps to where the execution was resumed. |
static void |
cancel()
Jumps to where the execution was resumed, and suspend execution. |
static Continuation |
continueWith(Continuation pOldContinuation)
Resumes the execution of the specified continuation from where it's left off. |
static Continuation |
continueWith(Continuation pOldContinuation,
java.lang.Object pContext)
Resumes the execution of the specified continuation from where it's left off and creates a new continuation representing the new state. |
static void |
exit()
Completes the execution of the running continuation. |
static java.lang.Object |
getContext()
get the current context. |
boolean |
isSerializable()
|
static Continuation |
startSuspendedWith(java.lang.Runnable pTarget)
Creates a new Continuation object from the specified Runnable
object. |
static Continuation |
startWith(java.lang.Runnable pTarget)
Starts executing the specified Runnable object in an environment
that allows suspend() . |
static Continuation |
startWith(java.lang.Runnable pTarget,
java.lang.Object pContext)
Starts executing the specified Runnable object in an environment
that allows suspend() . |
static void |
suspend()
Stops the running continuation. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Method Detail |
---|
public static java.lang.Object getContext()
This method returns the same context object given to startWith(Runnable, Object)
or continueWith(Continuation, Object)
.
A different context can be used for each run of a continuation, so this mechanism can be used to associate some state with each execution.
startWith(Runnable, Object)
or continueWith(Continuation, Object)
.public static Continuation startSuspendedWith(java.lang.Runnable pTarget)
Continuation
object from the specified Runnable
object.
Unlike the startWith(Runnable)
method, this method doesn't actually
execute the Runnable object. It will be executed when
it's continued
.
public static Continuation startWith(java.lang.Runnable pTarget)
Runnable
object in an environment
that allows suspend()
.
This is a short hand for startWith(target,null).
startWith(Runnable, Object).
public static Continuation startWith(java.lang.Runnable pTarget, java.lang.Object pContext)
Runnable
object in an environment
that allows suspend()
.
This method blocks until the continuation suspends or completes.
pTarget
- The object whose run method will be executed.pContext
- This value can be obtained from getContext()
until this method returns.
Can be null.
suspended
, in which case
a new non-null continuation is returned.getContext()
public static Continuation continueWith(Continuation pOldContinuation)
This is a short hand for continueWith(resumed,null).
continueWith(Continuation, Object)
public static Continuation continueWith(Continuation pOldContinuation, java.lang.Object pContext)
pOldContinuation
- The resumed continuation to be executed. Must not be null.pContext
- This value can be obtained from getContext()
until this method returns.
Can be null.
suspended
, in which case
a new non-null continuation is returned.getContext()
public boolean isSerializable()
public static void suspend()
This method can be only called inside continueWith(org.apache.commons.javaflow.Continuation)
or startWith(java.lang.Runnable)
methods.
When called, the thread returns from the above methods with a new Continuation
object that captures the thread state.
java.lang.IllegalStateException
- if this method is called outside the continueWith(org.apache.commons.javaflow.Continuation)
or startWith(java.lang.Runnable)
methods.public static void exit()
This method can be only called inside continueWith(org.apache.commons.javaflow.Continuation)
or startWith(java.lang.Runnable)
methods.
When called, the thread returns from the above methods with null,
indicating that there's nothing more to continue.
This method is similiar to how System.exit(int)
works for JVM.
public static void again()
This method can be only called inside continueWith(org.apache.commons.javaflow.Continuation)
or startWith(java.lang.Runnable)
methods.
When called, the execution jumps to where it was resumed
(if the execution has never resumed before, from the beginning
of Runnable.run()
.)
Consider the following example:
Continuation.suspend(); System.out.println("resumed"); r = new Random().nextInt(5); if(r!=0) { System.out.println("do it again"); Continuation.again(); } System.out.println("done");
This program produces an output like this (the exact number of 'do it again' depends on each execution, as it's random.)
resumed do it again resumed do it again resumed do it again resumed done
The calling startWith(Runnable)
method and
continueWith(Continuation)
method does not
return when a program running inside uses this method.
public static void cancel()
This method almost works like the again()
method,
but instead of re-executing, this method first suspends the execution.
Therefore,
the calling startWith(Runnable)
method and
continueWith(Continuation)
method
return when a program running inside uses this method.
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |