Fibers and coroutines概念

转载 2016年08月31日 15:15:54

Fiber(直译:纤维,这里light-weight-thread 纤程比较合适…)


1.     Whatare fibers and why should you care?


2. How to Implementing coroutines in Java

First,what is coroutines?

Coroutines are computer program componentsthat generalize(概括) subroutines(子程序) for nonpreemptive(非抢占式的) multitasking, by allowing multiple entry(入口) pointsfor suspending and resuming execution at certain locations. Coroutines arewell-suited for implementing more familiar program components such ascooperative(合作的) tasks, exceptions, event loop, iterators, infinite lists andpipes.

According to Donald Knuth, the termcoroutine was coined by Melvin Conway in 1958, after he applied it toconstruction of an assembly program.[1] The first published explanation of thecoroutine appeared later, in 1963。


Distinct  language has distinct introduction and implementation,details see:


This question isrelated to my question on existing coroutine implementations in Java. If, as Isuspect, it turns out that there is no full implementation of coroutinescurrently available in Java, what would be required to implement them?


As I said in thatquestion, I know about the following:


You can implement"coroutines" as threads/thread pools behind the scenes.

You can do tricksy(狡猾的) things with JVM bytecode behind thescenes to make coroutines possible.

The so-called"Da Vinci Machine" JVM implementation has primitives that makecoroutines doable without bytecode manipulation(控制).

There are variousJNI-based approaches(方法,路径) to coroutines also possible.

I'll address eachone's deficiencies in turn.




This"solution" is pathological(极端的,病态的). The whole point of coroutines is to avoid theoverhead of threading, locking, kernel scheduling, etc. Coroutines are supposedto be light and fast and to execute only in user space. Implementing them interms of full-tilt threads with tight restrictions gets rid of all theadvantages.


JVM bytecodemanipulation


This solution ismore practical(注重实际的), albeit(虽然) a bit difficult to pull off(完成). This is roughly(大体,大致上) the same as jumping down into assembly language forcoroutine libraries in C (which is how many of them work) with the advantagethat you have only one architecture to worry about and get right.


It also ties youdown to only running your code on fully-compliant JVM stacks (which means, forexample, no Android) unless you can find a way to do the same thing on thenon-compliant stack. If you do find a way to do this, however, you have nowdoubled your system complexity(复杂性) and testing needs.


The Da VinciMachine(build a patched JVM Machine)


The Da VinciMachine is cool for experimentation, but since it is not a standard JVM itsfeatures aren't going to be available everywhere. Indeed I suspect mostproduction environments would specifically forbid the use of the Da VinciMachine. Thus I could use this to make cool experiments but not for any code Iexpect to release to the real world.


This also has theadded problem similar to the JVM bytecode manipulation solution above: won'twork on alternative stacks (like Android's).


JNI implementation


This solution renders the point of doing this inJava at all moot. Each combination of CPU and operating system requiresindependent testing and each is a point of potentially frustrating subtlefailure. Alternatively, of course, I could tie myself down to one platformentirely but this, too, makes the point of doing things in Java entirely moot.


在Ruby on Rails和NodeJS开发者之间曾经引起宗教类的口水战:顺序编程风格 Vs 基于事件编程。目前大部分Web应用包括Ruby on Rails, Java Spring, Djang...
  • qizhiqq
  • qizhiqq
  • 2017年05月04日 14:08
  • 561

Windows Via C/C++ 读书笔记 8 Fiber(纤程)

Windows Via C/C++ 读书笔记 8 Fiber(纤程)1. Overview本章讲UNIX多线程和Windows线程区别,和Fibers的相似。Unix部分是我查资料加个人理解写的,可能...
  • itecwwj
  • itecwwj
  • 2009年06月10日 09:58
  • 2594


function demo(key, value) { var fiber = Fiber.current; client.set(key, value, function(e...
  • dongsoso
  • dongsoso
  • 2014年10月28日 16:35
  • 1672


在kotlin1.1,Coroutines还处于实验阶段。 有些API启动耗时操作比如:网络IO,文件流IO,CPU/GPU要求高的工作,它们会导致调用阻塞直到操作完成。Coroutines通过一种...
  • ncuboy045wsq
  • ncuboy045wsq
  • 2017年06月01日 17:41
  • 489

node.js异步转同步 fibers模块

  • fjs_cloud
  • fjs_cloud
  • 2013年04月21日 13:05
  • 6462


Unity里的Coroutine在帮助我们实现序列化事件时尤其方便。可以让事件依次发生,可以让事件A等待事件B结束后才开始执行。 但需要厘清几个基本概念。 Coroutines不是多线程,不是异步技术...
  • u010153703
  • u010153703
  • 2014年08月14日 13:13
  • 1418

c++ boost coroutine

For thread, the os help us schedule the thread automatically. For coroutine, we need to schedule th...
  • johnchung123
  • johnchung123
  • 2017年01月24日 19:00
  • 317

《Kotlin 程序设计》第十二章 Kotlin的多线程:协程(Coroutines)

第十二章 Kotlin的多线程:协程(Coroutines) Kotlin 1.1 introduced coroutines, a new way of writing asynchronous, ...
  • universsky2015
  • universsky2015
  • 2017年09月13日 14:32
  • 255

Kotlin学习之-6.4 Coroutines

Kotlin学习之-6.4 Coroutines 在Kotlin V1.1中Coroutines 还处在实验阶段 有些接口会做一些耗时的操作例如网络IO请求,文件IO, CPU或者GUP密集的工作...
  • farmer_cc
  • farmer_cc
  • 2017年07月21日 09:24
  • 1120

The Fibers of Threads

For the last several months in this column, weve been looking at programming with Linuxs threads lib...
  • coofive
  • coofive
  • 2006年08月23日 19:09
  • 893
您举报文章:Fibers and coroutines概念