【协程原理】 - Java中的协程

很长一段时间,我都很天真的认为python,特别是以gevent为代表的库,才是协程的乐土。Java里是没法实现协程,更别说实现stackless python这样可以pickle的协程的。Bong!咱真的是太井底之蛙了。
Java不但可以实现协程,而且还有很多个实现版本。不完全列表如下:

  • PicoThreads
    http://research.microsoft.com/en-us/um/people/abegel/cs262.pdf
  • RIFE
    http://rifers.org/wiki/display/RIFE/Web%20continuations.html
  • Javaflow
    http://commons.apache.org/sandbox/commons-javaflow/
  • Matthias Mann's
    http://hg.l33tlabs.org/Continuations/
    http://docs.paralleluniverse.co/quasar/
    http://indiespot.net/files/projects/continuationslib/

还有一个据(作者)说是最NB的kilim (https://github.com/kilim/kilim)
这些协程库的实现方式都是类似的,都是通过jvm字节码生成达到pause/resume的目的。在这篇文章中,RIFE的作者很清楚地讲明白了其实现方式:
http://www.artima.com/lejava/articles/continuations.html

Geert Bevin: At the byte-code level, when the method pops variables
from the stack, exactly the same operation will be performed on the
parallel stack. We just add a piece of byte code that mimics what goes
on exactly. Then, when a continuation has to be resumed, there's a bit
of added byte-code that interacts with the state of the stack, and
puts all the variables in the correct location so that they are
present when the executing thread passes that point. At this point,
it's as if nothing happened—execution resumed as if nothing happened.

The second problem is how to arrive at the point to resume a
continuation. You want to skip over code that you don't want to
execute. That's easily done in byte code, because you can maintain a
tableswitch in byte code that allows you to jump to a particular
location. You can create a unique label for each continuation point,
jump to that label in the switch table, and then you know exactly what
the stack for that label of variables was. You can restore that stack,
and continue executing from that point on.

kiliam的作者用代码解释得更加清楚(http://www.malhar.net/sriram/kilim/thread_of_ones_own.pdf):

// original
void a() throws Pasuable {
    x = ...
    b(); // b is pausable
    print (x);
}

经过代码增强之后是

// transformed code
void a(Fiber f) {
    switch (f.pc) { // prelude
        case 0: goto START;
        case 1: goto CALL_B}
    START:
    x = ...
    CALL_B: // pre_call
    f.down()
    b(f);
    f.up() // post-call
    switch (f.status) {
        case NOT_PAUSING_NO_STATE:
            goto RESUME
        case NOT_PAUSING_HAS_STATE:
            restore state
            goto RESUME
        case PAUSING_NO_STATE :
            capture state, return
        case PAUSING_HAS_STATE:
            return
    }
    RESUME:
    print (x);
}

因为这些框架都是以java对象的方式来存储call stack state和programe counter的,所以要做到序列化存储一个执行中的状态(continuation)一点都不困难。RIFE在其web框架就狠狠地利用了clonable的状态来实现复杂的wizard(回到任意时刻地过去重新执行之类的)。
看过了这些实现之后,我不禁觉得持久化一个continuation好像没啥了不起的,为什么会是stackless python的pypy两家的独门绝技呢?基于CPython的两个协程实现,一个greenlet一个fibers是否可以实现状态的持久化?状态持久化能不能不用更高效的serializer来实现呢?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值