title: Available Coroutine Libraries in Java
body: <p>I would like to do some stuff in Java that would be clearer if written using concurrent routines, but for which full-on threads are serious overkill.  The answer, of course, is the use of <a href="http://en.wikipedia.org/wiki/Coroutine" rel="noreferrer" title="Wikipedia on coroutines">coroutines</a>, but there doesn't appear to be any coroutine support in the standard Java libraries and a quick Google on it brings up tantalising hints here or there, but nothing substantial.</p>
  
  <p>Here's what I've found so far:</p>
  
  <ul>
  <li><a href="http://www.cs.uga.edu/~jam/jsim/JSIM/docs/jsim/coroutine/Coroutine.html" rel="noreferrer" title="JSIM web simulation library">JSIM</a> has a coroutine class, but it looks pretty heavyweight and conflates, seemingly, with
   threads at points.  The point of this is to reduce the complexity of full-on threading, not to add to
   it.  Further I'm not sure that the class can be extracted from the library and used independently.</li>
  <li><a href="http://xml.apache.org/xalan-j/apidocs/org/apache/xml/dtm/ref/CoroutineManager.html" rel="noreferrer" title="Xalan XML processing library">Xalan</a> has a coroutine set class that does coroutine-like stuff, but again it's dubious if this
   can be meaningfully extracted from the overall library.  It also looks like it's implemented as a
   tightly-controlled form of thread pool, not as actual coroutines.</li>
  <li>There's a <a href="http://code.google.com/p/coroutines/wiki/UserGuide" rel="noreferrer" title="coroutines for Java">Google Code project</a> which looks like what I'm after, but if anything it looks more
   heavyweight than using threads would be.  I'm basically nervous of something that requires software to
   dynamically change the JVM bytecode at runtime to do its work.  This looks like overkill and like
   something that will cause more problems than coroutines would solve.  Further it looks like it doesn't
   implement the whole coroutine concept.  By my glance-over it gives a <code>yield</code> feature that just returns
   to the invoker.  Proper coroutines allow <code>yield</code>s to transfer control to any known coroutine directly.
   Basically this library, heavyweight and scary as it is, only gives you support for iterators, not
   fully-general coroutines.</li>
  <li>The promisingly-named <a href="http://www.nevaobject.com/_docs/_coroutine/coroutine.htm" rel="noreferrer" title="Coroutine for Java">Coroutine for Java</a> fails because it's a platform-specific (obviously using
   JNI) solution.</li>
  </ul>
  
  <p>And that's about all I've found.</p>
  
  <p>I know about the native JVM support for coroutines in the Da Vinci Machine and I also know about the <a href="https://docs.google.com/viewer?url=http://wiki.jvmlangsummit.com/images/2/2b/JVMLanguageSummit_Stadler_Continuations.pdf" rel="noreferrer" title="JVM continuations">JNI continuations trick</a> for doing this.  These are not really good solutions for me, however, as I would not necessarily have control over which VM or platform my code would run on.  (Indeed any bytecode manipulation system would suffer similar problems -- it would be best were this pure Java if possible.  Runtime bytecode manipulation would restrict me from using this on Android, for example.)</p>
  
  <p>So does anybody have any pointers?  Is this even possible?  If not, will it be possible in Java 7?</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/2846664/implementing-coroutines-in-java" title="StackOverflow: Implementing Coroutines in Java">my other one</a>, but not the same.  This one is looking for an <strong>existing</strong> implementation in a bid to avoid reinventing the wheel unnecessarily.  The other one is a question relating to how one would go about implementing coroutines in Java should this question prove unanswerable.  The intent is to keep different questions on different threads.</p>
  
  <hr>
  
  <p><em>Further edited to add:</em></p>
  
  <p>The <a href="https://stackoverflow.com/questions/2846428/available-coroutine-libraries-in-java/2846980#2846980">answer is selected</a>.  Some commentary, however, is in order.  The library pointed to is not a coroutine library, so it technically doesn't answer my question.  That being said, however, it has two edges over the Google Code project linked to above:</p>
  
  <ol>
  <li>Both solutions use bytecode manipulation, but the selected library allows <em>static</em> bytecode manipulation which renders it usable in Android and other non-compliant JVM stacks.</li>
  <li>The Google Code project doesn't do full coroutines.  While the answer's library doesn't even do coroutines at all, it does something more important: it provides a good, foundational tool for rolling my own full-featured coroutines.</li>
  </ol>