Available Coroutine Libraries in Java
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 coroutines, 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.
Here's what I've found so far:
- JSIM 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.
- Xalan 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.
- There's a Google Code project 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
yield
feature that just returns to the invoker. Proper coroutines allowyield
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. - The promisingly-named Coroutine for Java fails because it's a platform-specific (obviously using JNI) solution.
And that's about all I've found.
I know about the native JVM support for coroutines in the Da Vinci Machine and I also know about the JNI continuations trick 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.)
So does anybody have any pointers? Is this even possible? If not, will it be possible in Java 7?
Edited to add:
Just to ensure that confusion is contained, this is a related question to my other one, but not the same. This one is looking for an existing 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.
Further edited to add:
The answer is selected. 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:
- Both solutions use bytecode manipulation, but the selected library allows static bytecode manipulation which renders it usable in Android and other non-compliant JVM stacks.
- 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.
Javaflow is a continuation implementation, it will probably let you do that. It uses bytecode manipulation though.
Anyway, it feels like you're trying to do OOP with plain C. It's doable but it doesn't mean you should do it.
The Kilim framework implements coroutines by using byte code rewriting. I've used it myself to implement light-weight processes in Erjang, and it is very stable and surprisingly fast for the amount of bytecode rewriting that goes on.
Kilim's coroutines interact by using mailboxes, so I use the framework to model Erlang actors. But it can just as well be used to do coroutines in a shared memory model.
What do you think of this continuations library written by Matthias Mann? I have copied the pros and cons from the creator's web site to facilitate discussion. It is important to look at the tests in the source code to see beyond the one example on the web site.
http://www.matthiasmann.de/content/view/24/26/
Lets start with what you get:
- Write simple sequential code - you no longer need to create state machines by hand
- No Threads are created or needed - no multi thread synchronization issues
- No garbage creation from code execution
- Very small runtime overhead
- Only suspendable method calls are changed - all calls into your standard library (like java.util.* etc) are not affected at all.
- Full serialization support
- You can store the execution state of coroutines as part of your game state in your save game without any additional code. This of course requires that your classes and data types which you use in your coroutines are serializable. Full support for exception handling and finally blocks
- Offline preprocessing does not slow down you application load time Of course runtime instrumentation is also possible.
- Very small runtime library - less then 10 KByte (uncompressed JAR) BSD License
With all these great features - you may be asking for the drawbacks. Well there are of course a few drawbacks:
- Constructors and static initializers can't be suspended
- Suspendable methods can't be synchronized or have synchronized blocks
- You need to download ASM3 library to run the instrumentation task
- You can't call suspendable method with reflection
The synchronization issue can be worked around by putting code which requires the use of synchronization into it's own method.
Your requirements seem to be:
- lightweight - not based on Threads,
- no reliance on native code, and
- no use of bytecode modification.
I have a nasty feeling that these requirements have ruled out all sensible strategies for implementing coroutines in Java.