Implementing coroutines in Java

Posted by JUST MY correct OPINION on Stack Overflow See other posts from Stack Overflow or by JUST MY correct OPINION
Published on 2010-05-17T04:26:00Z Indexed on 2010/05/17 4:30 UTC
Read the original article Hit count: 262

Filed under:
|
|

This question is related to my question on existing coroutine implementations in Java. 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?

As I said in that question, I know about the following:

  1. You can implement "coroutines" as threads/thread pools behind the scenes.
  2. You can do tricksy things with JVM bytecode behind the scenes to make coroutines possible.
  3. The so-called "Da Vinci Machine" JVM implementation has primitives that make coroutines doable without bytecode manipulation.
  4. There are various JNI-based approaches to coroutines also possible.

I'll address each one's deficiencies in turn.

Thread-based coroutines

This "solution" is pathological. The whole point of coroutines is to avoid 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.

JVM bytecode manipulation

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.

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.

The Da Vinci Machine

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.

This also has the added problem similar to the JVM bytecode manipulation solution above: won't work on alternative stacks (like Android's).

JNI implementation

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.

So...

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about coroutine