title: Implementing coroutines in Java
body: <p>This question is related to my question on <a href="https://stackoverflow.com/questions/2846428/coroutines-in-java" title="StackOverflow: Coroutines in Java">existing coroutine implementations in Java</a>.  If, as I suspect, it turns out that there is no full implementation of coroutines currently available in Java, what would be required to implement them?</p>
  
  <p>As I said in that question, I know about the following:</p>
  
  <ol>
  <li>You can implement "coroutines" as threads/thread pools behind the scenes.</li>
  <li>You can do tricksy things with JVM bytecode behind the scenes to make coroutines possible.</li>
  <li>The so-called "Da Vinci Machine" JVM implementation has primitives that make coroutines doable without
    bytecode manipulation.</li>
  <li>There are various JNI-based approaches to coroutines also possible.</li>
  </ol>
  
  <p>I'll address each one's deficiencies in turn.</p>
  
  <h2>Thread-based coroutines</h2>
  
  <p>This "solution" is pathological.  The whole point of coroutines is to <em>avoid</em> the overhead of threading, locking, kernel scheduling, etc.  Coroutines are supposed to be light and fast and to execute only in user space.  Implementing them in terms of full-tilt threads with tight restrictions gets rid of all the advantages.</p>
  
  <h2>JVM bytecode manipulation</h2>
  
  <p>This solution is more practical, albeit a bit difficult to pull off.  This is roughly the same as jumping down into assembly language for coroutine libraries in C (which is how many of them work) with the advantage that you have only one architecture to worry about and get right.</p>
  
  <p>It also ties you down to only running your code on fully-compliant JVM stacks (which means, for example, no Android) unless you can find a way to do the same thing on the non-compliant stack.  If you do find a way to do this, however, you have now doubled your system complexity and testing needs.</p>
  
  <h2>The Da Vinci Machine</h2>
  
  <p>The Da Vinci Machine is cool for experimentation, but since it is not a standard JVM its features aren't going to be available everywhere.  Indeed I suspect most production environments would specifically forbid the use of the Da Vinci Machine.  Thus I could use this to make cool experiments but not for any code I expect to release to the real world.</p>
  
  <p>This also has the added problem similar to the JVM bytecode manipulation solution above: won't work on alternative stacks (like Android's).</p>
  
  <h2>JNI implementation</h2>
  
  <p>This solution renders the point of doing this in Java at all moot.  Each combination of CPU and operating system requires independent testing and each is a point of potentially frustrating subtle failure.  Alternatively, of course, I could tie myself down to one platform entirely but this, too, makes the point of doing things in Java entirely moot.</p>
  
  <p>So...</p>
  
  <p>Is there any way to implement coroutines in Java without using one of these four techniques?  Or will I be forced to use the one of those four that smells the least (JVM manipulation) instead?</p>
  
  <hr>
  
  <p><em>Edited to add:</em></p>
  
  <p>Just to ensure that confusion is contained, this is a <em>related</em> question to <a href="https://stackoverflow.com/questions/2846428/coroutines-in-java" title="StackOverflow: Coroutines in Java">my other one</a>, but not the same.  That one is looking for an <strong>existing</strong> implementation in a bid to avoid reinventing the wheel unnecessarily.  This one is a question relating to how one would go about implementing coroutines in Java should the other prove unanswerable.  The intent is to keep different questions on different threads.</p>