2019独角兽企业重金招聘Python工程师标准>>>

Java 要在新的JDK版本中将支持协程,java loom project ,这个project的发起人正是Quasar的作者Ron. 相关的概念在里面都有解释。

Fiber 协程(轻量级用户态线程),后文统称为fiber。

fiber怎样执行

Disclaimer: this is a rough and inaccurate description of the scheduling in the kernel and in the go runtime aimed at explaining the concepts, not at being an exact or detailed explanation of the real system.

As you may (or not know), a CPU can't actually run two programs at the same time: a CPU only have one execution thread, which can execute one instruction at a time. The direct consequence on early systems was that you couldn't run two programs at the same time, each program needing (system-wise) a dedicated thread.

The solution currently adopted is called pseudo-parallelism: given a number of logical threads (e.g multiple programs), the system will execute one of the logical threads during a certain amount of time then switch to the next one. Using really small amounts of time (in the order of milliseconds), you give the human user the illusion of parallelism. This operation is called scheduling.

The Go language doesn't use this system directly: it itself implement a scheduler that run on top of the system scheduler, and schedule the execution of the goroutines itself, bypassing the performance cost of using a real thread for each routine. This type of system is called light/green thread.

上面描述的是基于fiber的并发模型,fiber是一段连续的指令,可以被暂停和恢复,被CPU调度。体现效率的原因是CPU调度fiber的时候可以连续不断的执行多个fiber不间断。

对比多线程,比如java的1:1线程模型。多个线程要不断的进行切换产生额外的context swicth开销,特别是当线程数量特别大的时候。

当fiber遇到system call(系统调用,需要切换用户态至内核态),会另外启用一个内核线程执行内核相关操作,完成之后回调通知这个fiber,可以继续被CPU调度了。请参考Go scheduler: Ms, Ps & Gs

关于fiber的调度,请参考这篇论文Analysis of the Go runtime scheduler 和 这篇博客Go's work-stealing scheduler

fiber和IO

请先参考这篇博客The Go netpoller ,这里描述了fiber io的编程模型。下面我们通过代码的形式分析一下IO操作怎么高效执行的。

IO模型: BIO: per connection per thread. 处理能力和thread数量相关。
NIO:reactor + handler, 处理能力 ( Limited by CPU, bandwidth, File descriptors (not threads))

编程模型:

aync thread model

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {try {// do some block io request.Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}return "result";});

这是我们通常的异步操作 + 阻塞IO模型,这个IO操作的并发量就看线程池的大小,而当线程池太大设计太大的时候就会给系统带来其他的问题,cpu 会花费大量时间在线程切换上。

fiber model

new Fiber<Void>(new SuspendableRunnable() {public void run() throws SuspendExecution, InterruptedException {try {     //do some block io operationThread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}bar(); // call bar;}
}).start();

而fiber则可以同时创建百万级别,并发量能和socket一个级别。这就是当并发量大之后的性能优势。同时能编写同步调用的代码。

对比netty.

Netty 基于异步IO模型 + 事件驱动 的网络IO框架,目标也是充分利用CPU。
在网络请求方面使用Netty,和使用Fiber+BIO都可以达到非阻塞的效果,具体性能测试,后续找机会测。

同步和异步编程模型

所以就衍化出了两种编程模式

  1. Reactive 模式,也就是 Spring5支持的响应式编程 WebFlux + Netty编程栈。
  2. Fiber 模式,因为fiber还在JDK之后的版本开发中,可以看下Quasar,文档里有完整的支持方案。

转载于:https://my.oschina.net/tigerlene/blog/2395872

Fiber 为什么做并发IO的时候更加高效相关推荐

  1. Ruby的Fiber根本不是用来做并发的~

    为什么80%的码农都做不了架构师?>>>    本来做了一个并发抓取,以为Ruby1.9以后添加的Fiber是类似于golang那种,可以实现并发运行,可是发现效率没有提高,为了确认 ...

  2. PHP并发IO编程之路

    并发IO问题一直是服务器端编程中的技术难题,从最早的同步阻塞直接Fork进程,到Worker进程池/线程池,到现在的异步IO.协程.PHP程序员因为有强大的LAMP框架,对这类底层方面的知识知之甚少, ...

  3. 我为什么喜欢用C#来做并发编程

    题记:就语言和运行时层面,C#做并发编程一点都不弱,缺的是生态和社区. 硅谷才女朱赟(我的家门)昨天发了一篇文章<为什么用 Java -- 关于并发编程>,让大家学习了Java中如何进行并 ...

  4. Java IO: 并发IO

    转载自   Java IO: 并发IO 译文链接 作者: Jakob Jenkov 译者: 李璟 有时候你可能需要并发地处理输入和输出.换句话说,你可能有超过一个线程处理输入和产生输出.比如,你有一个 ...

  5. Go十大常见错误第7篇:不使用-race选项做并发竞争检测

    前言 这是Go十大常见错误系列的第7篇:不使用-race选项做并发竞争检测.素材来源于Go布道者,现Docker公司资深工程师Teiva Harsanyi. 本文涉及的源代码全部开源在:Go十大常见错 ...

  6. 高并发IO的底层原理

    从基础讲起,IO的原理和模型是隐藏在编程知识底下的,是开发人员必须掌握的基础原理,是基础的基础,更是通关大公司面试的必备知识. 本章从操作系统的底层原理入手.通过图文并茂的方式,为大家深入剖析高并发I ...

  7. 《Netty、Redis、Zookeeper高并发实战》2️⃣高并发IO的底层原理

    文章目录 1.IO读写的基础原理 1.1 内核缓冲区与进程缓存区 1.2 详解典型的系统调用流程 2.四种主要的IO模型 2.1 同步阻塞IO(Blocking IO) 2.2 同步非阻塞IO(Non ...

  8. php抢红包 并发 超时,处理高并发 IO瓶颈解决红包程序

    解决高并发 io瓶颈解决红包程序 本程序模拟的红包抽奖模式.总金额100元,随机用户获得1-10元的红包,直到红包分发完. redis中luckMoneyMax需要提前设置 $r->set('l ...

  9. 掌握4要素,做喜剧短视频剧本简单高效

    如今短视频的浪潮可谓是十分火热,有的人通过短视频获得了人生的第一桶金,更有人凭借短视频迅速大火了起来. 而如今,5G"来势汹汹",短视频也将会因此被推向高潮,如果你足够有眼界,那么 ...

最新文章

  1. Ubuntu上nfs的安装配置
  2. WinError 145] 目录不是空的
  3. KOFLive Beta Daily-Scrum 8
  4. Ubuntu上安装flashplayer
  5. 全球及中国绿色建筑产业规模现状与未来走势分析报告2022版
  6. 怎么查看电脑多少内核和多少逻辑处理器?
  7. VTK:Rendering之Cone4
  8. [TCP/IP] SSL的通讯原理
  9. 1、jeecg 笔记开篇
  10. 争分夺秒:阿里实时大数据技术全力助战双11
  11. 手机可以实现利用putty来管理Linux服务器
  12. 关于MultiActionController异步Ajax,post;
  13. 利用Python制作微信跳一跳外挂,又来带你装一波X!
  14. qgis导出shp_使用QGIS将文本坐标转换为矢量文件
  15. Altium Designer原理图与PCB设计学习笔记6——AD如何在多个原理图中查找相同的网络标号
  16. Android底层网络防火墙,Android系统中实现网络防火墙的方法
  17. 职位名称: Java技术经理
  18. html 修改表格行背景,HTML表格标记教程(20):行的背景色属性BGCOLOR
  19. vscode预览html插件,VSCode插件推荐-VSCode内嵌浏览器插件-Browser Preview
  20. 小白打boss之路——2020fintech训练营数据赛道

热门文章

  1. Android左边有固定文字的EditText
  2. 微信小程序 解决请求服务器手机预览请求不到数据的方法
  3. java垃圾回收机制_乐字节Java|GC垃圾回收机制、package和import语句
  4. Storm和MR及Spark Streaming的区别
  5. LeetCode 2 两数相加
  6. android中实现返回首页功能
  7. JavaScript中函数文档注释
  8. BeanShell用法汇总(部分摘抄至网络)
  9. egg 自学入门demo分享
  10. DVWA1.9平台XSS小结