Java中可用的协程库 [英] Available Coroutine Libraries in Java

查看:298
本文介绍了Java中可用的协程库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java做一些事情,如果使用并发例程编写会更清楚,但是对于哪些完全线程严重过度杀伤。答案当然是使用 coroutines ,但是在标准的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 有一个协程类,但它看起来非常重量级,并且似乎与$相混淆b $ b点的线程。这样做的目的是降低全面线程的复杂性,而不是增加
    。此外,我不确定该类是否可以从库中提取并独立使用。

  • Xalan 有一个协程集类,它可以执行类似协程的东西,但同样它也是如果这个
    可以从整个库中有意义地提取出来,那就太可疑了。它看起来像是一个
    严格控制的线程池形式,而不是实际的协同程序。

  • 有一个 Google Code项目,它看起来就像我追求的那样,但如果有什么它看起来更像
    比使用线程重量级会。我基本上担心需要软件来
    在运行时动态更改JVM字节码来完成其工作。这看起来像矫枉过正,就像
    一样会导致比协同程序解决的更多问题。此外,它看起来不是
    实现整个协程概念。通过我的一瞥,它给出了一个 yield 的功能,只需向调用者返回
    。正确的协同程序允许 yield s将控制权直接转移到任何已知的协程。
    基本上这个库,重量级和可怕的,只给你支持迭代器,而不是
    完全通用的协同程序。

  • 有希望命名的 Coroutine for Java 失败,因为它是特定于平台的(显然使用
    JNI)解决方案。

  • 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 allow yields 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.

我知道Da Vinci Machine中对协同程序的原生JVM支持,我也知道 JNI延续技巧这样做。然而,对我来说这些并不是真正好的解决方案,因为我不一定能控制我的代码运行在哪个VM或平台上。 (事实上​​,任何字节码操作系统都会遇到类似的问题 - 如果可能的话,最好是纯Java。例如,运行时字节码操作会限制我在Android上使用它。)

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.)

那么有人有任何指针吗?这有可能吗?如果没有,是否可以在Java 7中使用?

So does anybody have any pointers? Is this even possible? If not, will it be possible in Java 7?

编辑添加:

为了确保包含混淆,这是与相关的问题/ implementation-coroutines-in-javatitle =StackOverflow:用Java实现协同程序>我的另一个,但不一样。这是为了避免不必要地重新发明轮子,正在寻找现有的实施方案。另一个是关于如何在Java中实现协程的问题,如果这个问题无法解决的话。目的是在不同的线程上保留不同的问题。

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.

进一步编辑添加:

答案被选中。然而,一些评论是有序的。图书馆指出的不是一个协程库,所以它在技术上不回答我的问题。尽管如此,它与上面链接的Google Code项目相比有两个优势:

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:


  1. 两种解决方案都使用字节码操作,但选中库允许静态字节码操作,这使得它可以在Android和其他不兼容的JVM堆栈中使用。

  2. Google Code项目不执行完整的协同程序。虽然答案的库甚至根本不做协同程序,但它确实更重要:它提供了一个很好的基础工具来滚动我自己的全功能协程。

  1. 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.
  2. 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 是继续实现,它可能会让你这样做。它虽然使用了字节码操作。

Javaflow is a continuation implementation, it will probably let you do that. It uses bytecode manipulation though.

无论如何,感觉你正在尝试用普通的C做OOP。这是可行的,但这并不意味着你应该这样做。

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.

这篇关于Java中可用的协程库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆