IOStrategies

使用 NIO 时,我们问的本质问题是,我们将如何处理发生在 NIO channel 上的特定 NIO 事件。
通常我们有两个选项:处理当前(选择器)线程中的 NIO 事件或将其传递给工作线程进行处理。

1. Worker-thread IOStrategy.

最有用的IOStrategy,其中选择器线程(Selector thread)将NIO事件处理委托给工作线程(worker threads)。

[外链图片转存失败(img-ZkSlPvoN-1569084348722)(./images/workerthread-strategy.png)]

该IOStrategy具有很好的可扩展性和安全性。我们可以根据需要更改选择器(selector)和工作(worker)线程池的大小,
并且不存在在特定NIO事件处理期间可能发生的某些问题会影响在同一选择器上注册的其他通道的风险。

2. Same-thread IOStrategy.

可能是最有效的IOStrategy。
与 work-thread IOStrategy不同,same-IOStrategy处理当前线程中的NIO事件,从而避免了昂贵的线程上下文切换。

由于我们可以调整选择器线程池的大小,所以此IOStrategy仍具有很好的可伸缩性,但是它确实有缺点。需要注意的是,
通道NIO事件处理不会阻塞或执行任何长时间的操作,因为它可能会阻塞对同一选择器上发生的其他NIO事件的处理。

3. Dynamic IOStrategy.

如前所述,worker-thread 和 same-thread 策略具有明显的优缺点。
但是,如果策略可以根据当前条件(负载 load,收集的统计信息 gathered statistics等)尝试在运行时智能地交换它们,该怎么办?

该 IOStrategy 可能会带来很多好处,并可以更好地控制资源。但是,重要的是不要使条件评估逻辑过于复杂,
因为它的复杂性将使IOStrategy与以前的两种策略相比效率低下。

4. Leader-follower IOStrategy.

Grizzly 2.3附带的最后一个IOStrategy是跟随者策略(leader-follower strategy):

该IOStrategy与 worker-thread IOStrategy相似,但不是将NIO事件处理传递给 worker thread,
而是把Selector对 selector-thread 的控制权传给work-thread,将工作线程更改为选择器线程,
并且实际的NIO事件处理在当前线程中进行。

Grizzly 2.3提供了通用接口org.glassfish.grizzly.IOStrategy:

package org.glassfish.grizzly;import org.glassfish.grizzly.strategies.WorkerThreadPoolConfigProducer;import java.io.IOException;
import java.util.concurrent.Executor;/*** <tt>strategy</tt> is responsible for making decision how* {@link Runnable} task will be run: in current thread, worker thread.** <tt>strategy</tt> can make any other processing decisions.* * @author Alexey Stashok*/
public interface IOStrategy extends WorkerThreadPoolConfigProducer {/*** The {@link org.glassfish.grizzly.nio.SelectorRunner} will invoke this* method to allow the strategy implementation to decide how the* {@link IOEvent} will be handled.** @param connection the {@link Connection} upon which the provided*  {@link IOEvent} occurred.* @param ioEvent the {@link IOEvent} that triggered execution of this*  <code>strategy</code>** @return <tt>true</tt>, if this thread should keep processing IOEvents on* the current and other Connections, or <tt>false</tt> if this thread* should hand-off the farther IOEvent processing on any Connections,* which means IOStrategy is becoming responsible for continuing IOEvent* processing (possibly starting new thread, which will handle IOEvents).** @throws IOException if an error occurs processing the {@link IOEvent}.*/boolean executeIoEvent(Connection connection, IOEvent ioEvent)throws IOException;/*** The {@link org.glassfish.grizzly.nio.SelectorRunner} will invoke this* method to allow the strategy implementation to decide how the* {@link IOEvent} will be handled.** @param connection the {@link Connection} upon which the provided*  {@link IOEvent} occurred.* @param ioEvent the {@link IOEvent} that triggered execution of this*  <code>strategy</code>* @param isIoEventEnabled <tt>true</tt> if IOEvent is still enabled on the*  {@link Connection}, or <tt>false</tt> if IOEvent was preliminary disabled*  or IOEvent is being simulated.** @return <tt>true</tt>, if this thread should keep processing IOEvents on* the current and other Connections, or <tt>false</tt> if this thread* should hand-off the farther IOEvent processing on any Connections,* which means IOStrategy is becoming responsible for continuing IOEvent* processing (possibly starting new thread, which will handle IOEvents).** @throws IOException if an error occurs processing the {@link IOEvent}.*/boolean executeIoEvent(Connection connection, IOEvent ioEvent,boolean isIoEventEnabled) throws IOException;/*** Returns an {@link Executor} to be used to run given <tt>ioEvent</tt>* processing for the given <tt>connection</tt>. A <tt>null</tt> value will* be returned if the <tt>ioEvent</tt> should be executed in the kernel thread.* * @param connection* @param ioEvent* @return an {@link Executor} to be used to run given <tt>ioEvent</tt>* processing for the given <tt>connection</tt>*/Executor getThreadPoolFor(Connection connection, IOEvent ioEvent);
}

IOStrategy实现可以决定如何处理特定的NIO事件。

根据上面的列表,Grizzly 2.3具有四个预定义的IOStrategy实现:

  • org.glassfish.grizzly.strategies.WorkerThreadIOStrategy
  • org.glassfish.grizzly.strategies.SameThreadIOStrategy
  • org.glassfish.grizzly.strategies.SimpleDynamicThreadStrategy
  • org.glassfish.grizzly.strategies.LeaderFollowerIOStrategy
    这些策略是按传输分配的,因此可以使用 Transport’s get/setIOStrategy 方法获取/设置 IOStrategy.
    TCP 和 UDP transports 默认使用 worker-thread IOStrategy.

grizzly 简化NIO事件开发相关推荐

  1. Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发

    Spring JDBC     * Spring框架对JDBC的简单封装.提供了一个JDBCTemplate对象简化JDBC的开发     * 步骤:         1. 导入jar包        ...

  2. Spring JDBC,JDBCTemplate对象简化JDBC的开发

    Spring JDBC,JDBCTemplate对象简化JDBC的开发 本篇文章中使用到了Druid数据库连接池,和自己创建的一个配合数据库连接池的工具类,本篇文章对于这些没有进行详细说明,对于这个, ...

  3. 用 GStreamer 简化 Linux 多媒体开发

    GStreamer 是 GNOME 桌面环境下用来构建流媒体应用的编程框架(framework),其目标是要简化音/视频应用程序的开发,目前已经能够被用来处理像 MP3.Ogg.MPEG1.MPEG2 ...

  4. 简化业务代码开发:看Lambda表达式如何将代码封装为数据

    摘要:在云服务业务开发中,善于使用代码新特性,往往能让开发效率大大提升,这里简单介绍下lambad表达式及函数式接口特性. 1.Lambda 表达式 Lambda表达式也被称为箭头函数.匿名函数.闭包 ...

  5. 微软驱动模块框架旨在简化Windows驱动开发

    使用微软最近新开源的微软驱动模块框架(DMF),Windows驱动开发者现在有一种更简单的方式创建简单的结构化驱动以及在驱动之间共享代码了. \\ 该框架诞生于Surface团队,微软DMF是传统Wi ...

  6. 微软开源 Tye 项目,可简化微服务开发

    微软近期开源了一款开发人员工具 Tye,能够用于简化微服务以及分布式应用程序的开发.测试以及部署过程.项目地址:https://github.com/dotnet/tye. 该项目负责人 Amiee  ...

  7. java nio 事件_Java NIO原理及实例

    Java NIO是在jdk1.4开始使用的,它既可以说成"新I/O",也可以说成非阻塞式I/O.下面是java NIO的工作原理: 1. 由一个专门的线程来处理所有的 IO 事件, ...

  8. java nio ppt_Java开发基础知识讲解.ppt

    Java开发基础;?目录第1章 Java体系结构;Java体系结构Java体系结;Java体系结构(续);?目录第1章 Java体系结构;大纲数据类型变量运算符字符串注;数据类型数据类型整型浮点型ch ...

  9. 【Visual C++】游戏开发笔记二十五 最简化的DirectX开发环境的配置

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7672101 作者:毛星云    邮箱: h ...

最新文章

  1. docker 主进程 日志_[docker]从一个实例,一窥docker进程管理
  2. 2016年计算机应用基础在线作业答案,2016电子科技大学计算机应用基础专科在线作业1...
  3. SQL 查询数据库中包含指定字符串的相关表和相关记录
  4. PoPo数据可视化第8期
  5. c++数字金字塔_“资金管理是投资最大的秘密”(超级干货),一生死记“金字塔加仓减仓法”,最安全稳健的操盘法方式!...
  6. redis zset usage
  7. 面对1.3 亿用户数据泄露,企业如何围绕核心数据构建安全管理体系?
  8. 手机MODEM 开发(29)--- VoLTE协议
  9. 网络协议分析-TCP协议分析
  10. DLNA和UPnP是什么关系?通俗解释
  11. 常用加密算法--对称加密算法
  12. 信息系统安全思维导图
  13. MySQL数据库创建表
  14. 2022-03-27 screenX和clientX的区别以及offsetX和pageX的区别
  15. 鸡兔同笼python
  16. 网站的文章怎么才能让百度等搜索引擎快速收录?
  17. android ios图片丢失,如何恢复iPhone、安卓智能手机上误删、丢失的图片?
  18. 甘油三酯偏高的原因与治疗方法
  19. uni-app 使用 Uview2.x 搭建自定义tabbar组件
  20. 【算法竞赛学习笔记】状压DP

热门文章

  1. 基于opencv3.0下的运动车辆检测
  2. 耗时三年,投入数千人 华为实现ERP自主可控 公司:“有史以来复杂性最高的项目”
  3. CA6140手柄轴的加工工艺及夹具设计(说明书+CAD图纸+三维图+工序卡……)
  4. 诛仙3每周几服务器维护,诛仙4月2日更新维护公告
  5. 嵌入式 C/C++语言精华文章集锦
  6. 等差、等比数列求和公式
  7. 一个等差×等比数列连加式
  8. 【学科介绍】计算机科学
  9. 框架-springmvc(ssm整合)
  10. 哪些ipad的APP可以很好的阅读英文文献?