Java Doc for CoroutineManager.java in  » XML » xalan » org » apache » xml » dtm » ref » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xalan » org.apache.xml.dtm.ref 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.xml.dtm.ref.CoroutineManager

CoroutineManager
public class CoroutineManager (Code)

Support the coroutine design pattern.

A coroutine set is a very simple cooperative non-preemptive multitasking model, where the switch from one task to another is performed via an explicit request. Coroutines interact according to the following rules:

  • One coroutine in the set has control, which it retains until it either exits or resumes another coroutine.
  • A coroutine is activated when it is resumed by some other coroutine for the first time.
  • An active coroutine that gives up control by resuming another in the set retains its context -- including call stack and local variables -- so that if/when it is resumed, it will proceed from the point at which it last gave up control.

Coroutines can be thought of as falling somewhere between pipes and subroutines. Like call/return, there is an explicit flow of control from one coroutine to another. Like pipes, neither coroutine is actually "in charge", and neither must exit in order to transfer control to the other.

One classic application of coroutines is in compilers, where both the parser and the lexer are maintaining complex state information. The parser resumes the lexer to process incoming characters into lexical tokens, and the lexer resumes the parser when it has reached a point at which it has a reliably interpreted set of tokens available for semantic processing. Structuring this as call-and-return would require saving and restoring a considerable amount of state each time. Structuring it as two tasks connected by a queue may involve higher overhead (in systems which can optimize the coroutine metaphor), isn't necessarily as clear in intent, may have trouble handling cases where data flows in both directions, and may not handle some of the more complex cases where more than two coroutines are involved.

Most coroutine systems also provide a way to pass data between the source and target of a resume operation; this is sometimes referred to as "yielding" a value. Others rely on the fact that, since only one member of a coroutine set is running at a time and does not lose control until it chooses to do so, data structures may be directly shared between them with only minimal precautions.

"Note: This should not be taken to mean that producer/consumer problems should be always be done with coroutines." Queueing is often a better solution when only two threads of execution are involved and full two-way handshaking is not required. It's a bit difficult to find short pedagogical examples that require coroutines for a clear solution.

The fact that only one of a group of coroutines is running at a time, and the control transfer between them is explicit, simplifies their possible interactions, and in some implementations permits them to be implemented more efficiently than general multitasking. In some situations, coroutines can be compiled out entirely; in others, they may only require a few instructions more than a simple function call.

This version is built on top of standard Java threading, since that's all we have available right now. It's been encapsulated for code clarity and possible future optimization.

(Two possible approaches: wait-notify based and queue-based. Some folks think that a one-item queue is a cleaner solution because it's more abstract -- but since coroutine _is_ an abstraction I'm not really worried about that; folks should be able to switch this code without concern.)

%TBD% THIS SHOULD BE AN INTERFACE, to facilitate building other implementations... perhaps including a true coroutine system someday, rather than controlled threading. Arguably Coroutine itself should be an interface much like Runnable, but I think that can be built on top of this.



Field Summary
final static  intANYBODY
    
final static  intNOBODY
    
 BitSetm_activeIDs
     "Is this coroutine ID number already in use" lookup table.
 intm_nextCoroutine
     Internal field used to confirm that the coroutine now waking up is in fact the one we intended to resume.
final static  intm_unreasonableId
     Limit on the coroutine ID numbers accepted.
 Objectm_yield
     Internal field used to hold the data being explicitly passed from one coroutine to another during a co_resume() operation. (Of course implicit data sharing may also occur; one of the reasons for using coroutines is that you're guaranteed that none of the other coroutines in your set are using shared structures at the time you access them.) %REVIEW% It's been proposed that we be able to pass types of data other than Object -- more specific object types, or lighter-weight primitives.


Method Summary
public synchronized  Objectco_entry_pause(int thisCoroutine)
     In the standard coroutine architecture, coroutines are identified by their method names and are launched and run up to their first yield by simply resuming them; its's presumed that this recognizes the not-already-running case and does the right thing.
public synchronized  voidco_exit(int thisCoroutine)
     Terminate this entire set of coroutines.
public synchronized  voidco_exit_to(Object arg_object, int thisCoroutine, int toCoroutine)
     Make the ID available for reuse and terminate this coroutine, transferring control to the specified coroutine.
public synchronized  intco_joinCoroutineSet(int coroutineID)
    

Each coroutine in the set managed by a single CoroutineManager is identified by a small positive integer.

public synchronized  Objectco_resume(Object arg_object, int thisCoroutine, int toCoroutine)
     Transfer control to another coroutine which has already been started and is waiting on this CoroutineManager.

Field Detail
ANYBODY
final static int ANYBODY(Code)



NOBODY
final static int NOBODY(Code)



m_activeIDs
BitSet m_activeIDs(Code)
"Is this coroutine ID number already in use" lookup table. Currently implemented as a bitset as a compromise between compactness and speed of access, but obviously other solutions could be applied.



m_nextCoroutine
int m_nextCoroutine(Code)
Internal field used to confirm that the coroutine now waking up is in fact the one we intended to resume. Some such selection mechanism is needed when more that two coroutines are operating within the same group.



m_unreasonableId
final static int m_unreasonableId(Code)
Limit on the coroutine ID numbers accepted. I didn't want the in-use table to grow without bound. If we switch to a more efficient sparse-array mechanism, it may be possible to raise or eliminate this boundary.



m_yield
Object m_yield(Code)
Internal field used to hold the data being explicitly passed from one coroutine to another during a co_resume() operation. (Of course implicit data sharing may also occur; one of the reasons for using coroutines is that you're guaranteed that none of the other coroutines in your set are using shared structures at the time you access them.) %REVIEW% It's been proposed that we be able to pass types of data other than Object -- more specific object types, or lighter-weight primitives. This would seem to create a potential explosion of "pass x recieve y back" methods (or require fracturing resume into two calls, resume-other and wait-to-be-resumed), and the weight issue could be managed by reusing a mutable buffer object to contain the primitive (remember that only one coroutine runs at a time, so once the buffer's set it won't be walked on). Typechecking objects is interesting from a code-robustness point of view, but it's unclear whether it makes sense to encapsulate that in the coroutine code or let the callers do it, since it depends on RTTI either way. Restricting the parameters to objects implementing a specific CoroutineParameter interface does _not_ seem to be a net win; applications can do so if they want via front-end code, but there seem to be too many use cases involving passing an existing object type that you may not have the freedom to alter and may not want to spend time wrapping another object around.





Method Detail
co_entry_pause
public synchronized Object co_entry_pause(int thisCoroutine) throws java.lang.NoSuchMethodException(Code)
In the standard coroutine architecture, coroutines are identified by their method names and are launched and run up to their first yield by simply resuming them; its's presumed that this recognizes the not-already-running case and does the right thing. We seem to need a way to achieve that same threadsafe run-up... eg, start the coroutine with a wait. %TBD% whether this makes any sense...
Parameters:
  thisCoroutine - the identifier of this coroutine, so we canrecognize when we are being resumed.
exception:
  java.lang.NoSuchMethodException - if thisCoroutine isn'ta registered member of this group. %REVIEW% whether this is thebest choice.



co_exit
public synchronized void co_exit(int thisCoroutine)(Code)
Terminate this entire set of coroutines. The others will be deregistered and have exceptions thrown at them. Note that this is intended as a panic-shutdown operation; under normal circumstances a coroutine should always end with co_exit_to() in order to politely inform at least one of its partners that it is going away. %TBD% This may need significantly more work. %TBD% Should this just be co_exit_to(,,CoroutineManager.PANIC)?
Parameters:
  thisCoroutine - Integer identifier for the coroutine requesting exit.



co_exit_to
public synchronized void co_exit_to(Object arg_object, int thisCoroutine, int toCoroutine) throws java.lang.NoSuchMethodException(Code)
Make the ID available for reuse and terminate this coroutine, transferring control to the specified coroutine. Note that this returns immediately rather than waiting for any further coroutine traffic, so the thread can proceed with other shutdown activities.
Parameters:
  arg_object - A value to be passed to the other coroutine.
Parameters:
  thisCoroutine - Integer identifier for the coroutine leaving the set.
Parameters:
  toCoroutine - Integer identifier for the coroutine we wish toinvoke.
exception:
  java.lang.NoSuchMethodException - if toCoroutine isn't aregistered member of this group. %REVIEW% whether this is the best choice.



co_joinCoroutineSet
public synchronized int co_joinCoroutineSet(int coroutineID)(Code)

Each coroutine in the set managed by a single CoroutineManager is identified by a small positive integer. This brings up the question of how to manage those integers to avoid reuse... since if two coroutines use the same ID number, resuming that ID could resume either. I can see arguments for either allowing applications to select their own numbers (they may want to declare mnemonics via manefest constants) or generating numbers on demand. This routine's intended to support both approaches.

%REVIEW% We could use an object as the identifier. Not sure it's a net gain, though it would allow the thread to be its own ID. Ponder.


Parameters:
  coroutineID - If >=0, requests that we reserve this number.If <0, requests that we find, reserve, and return an available IDnumber. If >=0, the ID number to be used by this coroutine. If <0,an error occurred -- the ID requested was already in use, or wecouldn't assign one without going over the "unreasonable value" mark



co_resume
public synchronized Object co_resume(Object arg_object, int thisCoroutine, int toCoroutine) throws java.lang.NoSuchMethodException(Code)
Transfer control to another coroutine which has already been started and is waiting on this CoroutineManager. We won't return from this call until that routine has relinquished control. %TBD% What should we do if toCoroutine isn't registered? Exception?
Parameters:
  arg_object - A value to be passed to the other coroutine.
Parameters:
  thisCoroutine - Integer identifier for this coroutine. This is theID we watch for to see if we're the ones being resumed.
Parameters:
  toCoroutine - Integer identifier for the coroutine we wish toinvoke.
exception:
  java.lang.NoSuchMethodException - if toCoroutine isn't aregistered member of this group. %REVIEW% whether this is the best choice.



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.