Available Coroutine Libraries in Java

最后都变了- 提交于 2019-11-27 02:46:14

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.

If you're using Java, there are 2 options available as of late 2017:

Both of these are based off of commons-javaflow -- they re-write your code at bytecode level to get things to work.

I maintain Coroutines -- The up-sides are that it's fast, it supports all major build systems, and it supports serialization/versioning of your coroutines. The down-side is that the API has a few deviations from commons-javaflow.

Vsilaev maintains Tasclate-Javaflow -- I haven't used it so I can't speak on it, but it is maintained and from looking at the examples it's API aligns closer to that of commons-javaflow.

There are also language features in Kotlin and Scala (and maybe other JVM-based languages) that let you use coroutines. But, before switching languages you should be aware that Kotlin, Scala, or whatever the JVM language du jour is today is not and will never be Java. Whatever it's doing in the background to make things work may not work when the next release of the JVM rolls around.

The folks who maintain the JDK at Oracle have a track record of using these third-party JVM languages as market research. If a high-level feature gets added to a third-party JVM language and it's popular enough, they'll incorporate it into Java. This is what's happening right now with coroutines. There's an OpenJDK project called Project Loom that aims to add coroutines to the Java language.

It's still early days for Project Loom. If you critically look through the proposal, it's a mess. I'm sure it'll stabilize as time goes on, but what we'll ultimately end up getting may be entirely different from what many of us are expecting.

To recap, your options are to either use one of the bytecode instrumentation toolkits or to switch languages. Project Loom is still in early days and there's a possibility that it may never actually get added to Java.

the Play framework now provides continuations with Javaflow. Because Play provides so much convenience in other areas, you may want to start with it.

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

There is a new coroutine framework library available for Java. It is implemented in pure Java, so there's no need for JNI or Java Agents running separately. It is open source and can be downloaded from GitHub:

https://github.com/esoco/coroutines

An introduction to the framework can be found on Medium:

https://medium.com/@esocogmbh/65661a379c85

Vadzim

Quasar implements Go-like coroutines and channels among other features using continuations.

More details, benchmarks and links on Quasar in my another answer.

I checked the related question you linked and for the life of me I can't really understand what's so bad about threads. In Java threads never are native threads, they're merely isolated execution units which depending on context may be running as their own native thread if it's beneficial, that is splitting large execution blocks to own threads is smart while keeping small ones in just a few is more sensible due to the overhead.

That said, Java/JDK doesn't natively have coroutines available for high-level programmers - yet. JDK7 (whenever that comes out) will have what's known as jsr166y which is a Fork/Join framework by Doug Lea. For technical information, check this PDF by mr. Lea himself. In practice, Fork/Join adds another (useful!) level of granularity on top of Java's internal threading model which should help you achieve what you want.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!