Continuations

来源:百度文库 编辑:神马文学网 时间:2024/04/28 17:15:06
Continuations refer to a functional programming technique that allows you to save the current execution state of a thread, with the possiblity of suspending and later resuming execution.
It can simplify complex workflows. Instead of creating an external control flow as individual steps that are tied together with navigation rules or state transitions, continuations provide a much simpler model—almost as simple as if you were doing console programming in the early DOS days.
With continuations, when you need to get a response from a user, you only need to say so: the execution of the server-side process will then halt and preserve everything that is going on for that particular process. When the next request from the user comes in, the server-side process resumes exactly where it left off before. It‘s just like when you save and resume a game.
Continuations free you from having to worry about where you are in the process, or what next steps you can take, or which elements of the session are entered into at what point in the execution.
The way that works is that continuations let you save the method call stack as well as the variable stack.
RIFE and Webwork support this concept.
Implementation in RIFE is to rewrite byte code to be loaded. API calls (pause()) to RIFE are acting as Markers. In the place of Marker, RIFE instruments some byte code to aggregate current state of method call stack and value stack. RIFE maintains a parallel stack referencing the actual data.
The bit of overhead that occurs is the result of the fact that you typically make a deep clone of the actual data at the next continuation. This is needed because each continuation needs to be totally independent of another one.
The only restrictions developers have in using continuations is that the data structure must be cloneable or serializable so that the infrastructure can create carbon copies of those data structures.
There is a continuation manager. It even would be statefully replicated in order to support stateful fail-over in the near future (in conjunction with Terracotta DSO ).
Continuation can let you save the state of execution.
It can be used to improve polling-based approach in a event-driven system. You don‘t need to poll manually. You only need an event to resume the execution of a continuation.
Special method call is pause(), call() and stepBack(). Pause()  you indicate one specific location you‘re interested in.
When pause() is called that continuation would stop, before that the content to user must have been flushed to sevlet container to let it render them for end-user. And the next request from the user will contain the continuation Id. The continuation manager will resume the previously stopped continuation.
http://www.artima.com/lejava/articles/continuations.html