Coroutines for Java

Hi,

I wrote a small (<10KB) library which adds Coroutines to Java :slight_smile:

A article and the library itself can be found here

Ciao Matthias

Itā€™s a very nice library, thanks for sharing. :slight_smile: Iā€™ve already integrated it into my engine for game code (and replaced JavaFlow that I used previously).

Very interesting. So, Iā€™m trying to grok it. What exactly does the preprocessing do? Can you show what happens to the TestIterator example?

Isnā€™t this like software threading, with ā€˜breakpointsā€™ scattered through the bytecodes allowing you to suspend a method, do some other stuff and then continue where you left? Is it? ;D

Iā€™m not too fond of something manipulating my bytecodes, but I guess in AI itā€™s very convenient - my state machines often turn into a steaming pile.

Well :slight_smile:

I could attach a javap dump of the example - which is long (at least too long for this forum) - but itā€™s very easy to generate - after running the JUnit tests all class files have been instrumented.

The resulting code is similar to the ā€œstandard Java exampleā€ on the website - except that the state is not stored as member variables - to allow for recursive calls - but instead in a thread local instance of the Stack class which is managed by the Coroutine class.

The bytecode gets more complex if you have exception handling using finally in your methods - even to the point where it will be impossible to write it using Java itself (without duplicating a lot of code).

Riven: Look at the possibilities that bytecode instrumentation offers ā€¦

Ciao Matthias