Available Coroutine Java Libraries

I would like to do something in Java that would be clearer if it were written using parallel procedures, but for which full-thread streams are serious excesses. The answer, of course, is to use coroutines , but there seems to be no coroutine support in standard Java libraries, and in quick Google there are tantalizing hints here or there, but nothing significant.

Here is what I have found so far:

  • JSIM has a coroutine class, but it looks rather heavy and complex, seemingly with threads in points. The point is to reduce the complexity of full-threading, rather than adding It to it. Further Iโ€™m not sure that the class can be extracted from the library and used independently.
  • Xalan has a coroutine suite class that runs accompanying materials, but again it is doubtful if it can be meaningfully extracted from a shared library. It also looks like executed as a tightly controlled form of a thread pool, and not as actual coroutines.
  • Here's the Google Code Project , which looks the way I follow it, but if something else looks harder than using streams. I am mostly nervous about something that requires software to dynamically change the JVM bytecode at runtime in order to do its job. This seems like overkill that will cause more problems than coroutines will solve. In addition, it does not seem to implement the whole concept of coroutine. With his gaze, he gives a yield function, which simply returns to the caller. Proper coroutines allow yield directly transfer control to any known coroutine. In principle, this library, heavy and scary, only gives you support for iterators, and not completely general coroutines.
  • The hopefully named Coroutine for Java fails because it is platform dependent (obviously using JNI).

And this is about everything that I found.

I know about the built-in JVM support for coroutines in the Da Vinci machine, and I also know about the JNI continuation trick for this. However, for me these are not very good decisions, since I will not necessarily control which VM or platform my code will work on. (Indeed, any bytecode manipulation system would face similar problems - it would be best to use this pure Java. Perhaps using bytecode in runtime would limit me to using this on Android.)

Does anyone have pointers? Is it possible? If not, is this possible in Java 7?




Edited to add:

Just to make sure that the confusion is contained, this is a different question for us, but not the same. In this case, you are looking for an existing implementation to avoid having to reinvent the wheel. Another question is the question of how it would be possible to implement coroutines in Java if this question turns out to be incontrovertible. The goal is to ask different questions on different topics.




Further edited to add:

Answer selected. However, some comments are fine. The library pointed out that this is not a coroutine library, so it does not technically answer my question. However, as said, it has two edges over the Google Code project linked above:

  • Both solutions use bytecode processing, but the selected library allows the use of static bytecode manipulation, which makes it suitable for use in Android and other inappropriate JVM packages.
  • The Google Code project does not run full coroutines. Although the answer library doesn't even run coroutines, it does something more important: it provides a good basic tool for transfusing my own full-featured coroutines.
+40
java coroutine
May 17 '10 at 3:03
source share
8 answers

Javaflow is a continuation implementation, it will probably allow you to do this. However, it does use bytecode manipulation.

In any case, it looks like you are trying to make OOP with simple C. This is doable, but that does not mean that you should do it.

+12
May 17 '10 at 6:14 a.m.
source share

Kilim framework implements coroutines using byte code rewriting. I myself used it to implement lightweight processes in Erjang , and it is very stable and surprisingly fast for the amount of bytecode rewriting this continues.

Kilim coroutines interact using mailboxes, so I use the framework to model Erlang actors. But it can also be used to execute coroutines in a shared memory model.

+10
May 25 '11 at 7:17
source share

What do you think of this sequel library written by Matthias Mann? I copied the pros and cons from the creator's website to facilitate discussion. It is important to look at the tests in the source code to see outside of one example on the website.

http://www.matthiasmann.de/content/view/24/26/

Let's start with what you get:

  • Write a simple serial code - you no longer need to create state machines manually
  • No threads are created or not needed - there is no problem synchronizing multiple threads.
  • No garbage creation from code execution
  • Very little runtime overhead
  • Only suspicious method calls have been changed - all calls to your standard library (for example, java.util. *, Etc.) are not affected at all.
  • Full serialization support
  • You can save coroutine execution status as part of the game state in your save game without additional code. This, of course, requires that your classes and the data types that you use in your coroutines be serializable. Full support for handling exceptions and finally blocks.
  • Continuous preprocessing does not slow down application loading time. Of course, you can use runtime tools.
  • Very small runtime library - less than 10 KB (uncompressed JAR) BSD license

With all these great features, you can ask for flaws. Well, of course, there are a few drawbacks:

  • Constructors and static initializers cannot be paused
  • Suspended methods cannot be synchronized or have synchronized blocks
  • You need to download the ASM3 library to run the test task.
  • You cannot call the suspendable method with reflection

The synchronization problem can be solved by putting code that requires the use of synchronization in its own method.

+9
Jan 14 2018-11-11T00:
source share

Your requirements are as follows:

  • lightweight - not based on Threads,
  • do not rely on native code and
  • no bytecode modification is used.

I have an unpleasant feeling that these requirements have ruled out all reasonable strategies for implementing coroutines in Java.

+5
May 17 '10 at 5:39 a.m.
source share

Playback structure now provides continuation with Javaflow. Since Play provides such convenience in other areas, you can start with it.

http://www.playframework.org/documentation/1.2RC2/releasenotes-1.2#Continuations

+2
Apr 09 '11 at 15:57
source share

Quasar implements Go-like coroutines and feeds among other features with sequel.

More details, tests and links to Quasar in my another answer .

0
Jun 13 '16 at 12:53 on
source share

If you use Java, at the end of 2017, 2 options are available:

Both are based on commons-javaflow - they rewrite your code at the bytecode level to make it work.

I support Coroutines - Surfaces is that it is fast, it supports all major build systems and supports serialization / versioning of your coroutines. The downside is that the API has a few deviations from commons-javaflow.

Vsilaev supports Tasclate-Javaflow - I didnโ€™t use it, so I canโ€™t talk about it, but it is also supported by viewing examples that the API aligns with commons-javaflow examples.

Kotlin also has language features and Scala (and possibly other JVM-based languages) that allow coroutines to be used. But before switching languages, you should know that Kotlin, Scala, or something like the du jour JVM language is not Java today and never will be. No matter what he does in the background, to make things work, it may not work when the next version of the JVM rolls.

People who support the JDK at Oracle have experience using these third-party JVM languages โ€‹โ€‹as market research. If a high-level function is added to a third-party JVM language and is quite popular, they will include it in Java. This is what is happening right now with coroutines. There is an OpenJDK project called Project Loom , which aims to add coroutines to the Java language.

These are the first days for Project Loom. If you critically review this offer, this is a mess. I am sure that it will stabilize over time, but what we ultimately get may be completely different from what many of us expect.

To repeat, your options should either use one of the bytecode toolkit tools or switch languages. Project Loom is still in the early days, and it is likely that it can never be added to Java.

0
Dec 27 '17 at 16:08
source share

I checked the question related to you and got in touch with life. I canโ€™t understand what is so bad about threads. Java threads never have their own threads, they are simply isolated execution units, which, depending on the context, can work as their own threads, if it is profitable, that is, splitting large blocks of execution into their own threads is smart, and small ones are only somewhat more reasonable due to overhead.

However, the Java / JDK does not have its own coroutines available to high-level programmers. JDK7 (whenever this comes out) will have what is called jsr166y , which is the Dork Lea Fork / Join structure. For technical information check this pdf at mr. Leah herself. In practice, Fork / Join adds another (useful!) Level of detail on top of the Java internal threading model, which should help you achieve what you want.

-2
May 18 '10 at 6:59 a.m.
source share



All Articles