本文主要研究一下reactive streams的schedulers

背景

默认情况下Mono以及Flux都在主线程上运行,有时候可能会阻塞主线程,可以通过设定schedulers让其在其他线程运行。

原始输出

没有使用publishOn及subscribeOn时输出如下

11:26:10.668 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
11:26:11.097 [main] INFO com.example.demo.SchedulerTest - defer thread:[main]
11:26:11.116 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:26:11.116 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:26:11.116 [main] INFO com.example.demo.SchedulerTest - subscribe thread:[main],data :2
11:26:11.116 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:26:11.116 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:26:11.117 [main] INFO com.example.demo.SchedulerTest - subscribe thread:[main],data :4
复制代码

publishOn(给subscriber配置线程)

   @Testpublic void testPublisherThread(){Scheduler pubScheduler = Schedulers.newSingle("pub-thread");Flux.defer(() -> {LOGGER.info("defer thread:[{}]",Thread.currentThread().getName());return Flux.range(1,4);}).filter(e -> {LOGGER.info("filter thread:[{}]",Thread.currentThread().getName());return e % 2 == 0;}).publishOn(pubScheduler).subscribe(e -> {LOGGER.info("subscribe thread:[{}],data :{}",Thread.currentThread().getName(),e);});}
复制代码

输出

11:31:23.691 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
11:31:23.871 [main] INFO com.example.demo.SchedulerTest - defer thread:[main]
11:31:23.880 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:31:23.881 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:31:23.881 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:31:23.881 [main] INFO com.example.demo.SchedulerTest - filter thread:[main]
11:31:23.881 [publisher-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-1],data :2
11:31:23.881 [publisher-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-1],data :4
复制代码

可以发现,配置publishOn,改变了subscribe的运行线程

subscribeOn(给publisher配置线程)

   @Testpublic void testSubscriberThread() throws InterruptedException {Scheduler subScheduler = Schedulers.newSingle("sub-thread");Flux.defer(() -> {LOGGER.info("defer thread:[{}]",Thread.currentThread().getName());return Flux.range(1,4);}).filter(e -> {LOGGER.info("filter thread:[{}]",Thread.currentThread().getName());return e % 2 == 0;}).subscribeOn(subScheduler).subscribe(e -> {LOGGER.info("subscribe thread:[{}],data :{}",Thread.currentThread().getName(),e);});Thread.sleep(10*1000);}
复制代码

输出如下:

11:31:58.294 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
11:31:58.528 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - defer thread:[subscriber-thread-1]
11:31:58.532 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:31:58.532 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:31:58.532 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[subscriber-thread-1],data :2
11:31:58.533 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:31:58.533 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:31:58.533 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[subscriber-thread-1],data :4
复制代码

可以发现,配置了subscribeOn,所有的都在这个线程运行,包括defer、包括filter、包括subscribe

publishOn和subscribeOn

 @Testpublic void testPublisherAndSubscriberThread() throws InterruptedException {Scheduler pubScheduler = Schedulers.newSingle("publisher-thread");Scheduler subScheduler = Schedulers.newSingle("subscriber-thread");Flux.defer(() -> {LOGGER.info("defer thread:[{}]",Thread.currentThread().getName());return Flux.range(1,4);}).filter(e -> {LOGGER.info("filter thread:[{}]",Thread.currentThread().getName());return e % 2 == 0;}).publishOn(pubScheduler).subscribeOn(subScheduler).subscribe(e -> {LOGGER.info("subscribe thread:[{}],data :{}",Thread.currentThread().getName(),e);});Thread.sleep(10*1000);}
复制代码

输出

11:33:00.964 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
11:33:01.125 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - defer thread:[subscriber-thread-1]
11:33:01.134 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:33:01.135 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:33:01.135 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:33:01.135 [subscriber-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[subscriber-thread-1]
11:33:01.135 [publisher-thread-2] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-2],data :2
11:33:01.135 [publisher-thread-2] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-2],data :4
复制代码

都配置了话,可以看到subscriber运行在publishOn配置的线程,而defer、filter等运行在subscribeOn配置的线程

publishOn及filter

  @Testpublic void testFilterThread(){Scheduler pubScheduler = Schedulers.newSingle("publisher-thread");Flux.defer(() -> {LOGGER.info("defer thread:[{}]",Thread.currentThread().getName());return Flux.range(1,4);}).publishOn(pubScheduler) //NOTE 注意这里放到了filter之前.filter(e -> {LOGGER.info("filter thread:[{}]",Thread.currentThread().getName());return e % 2 == 0;}).subscribe(e -> {LOGGER.info("subscribe thread:[{}],data :{}",Thread.currentThread().getName(),e);});}
复制代码

输出

13:19:01.606 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
13:19:01.754 [main] INFO com.example.demo.SchedulerTest - defer thread:[main]
13:19:01.766 [publisher-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[publisher-thread-1]
13:19:01.766 [publisher-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[publisher-thread-1]
13:19:01.766 [publisher-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-1],data :2
13:19:01.766 [publisher-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[publisher-thread-1]
13:19:01.766 [publisher-thread-1] INFO com.example.demo.SchedulerTest - filter thread:[publisher-thread-1]
13:19:01.767 [publisher-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-1],data :4
复制代码

这里将publishOn放在了filter之前,可以发现filter线程也变成publisher线程了 在publishOn之后的filter或map等将使用publishOn配置的线程;之前的话,使用的是main线程或subscribeOn配置的线程

subscribeOn及filter

将subscribeOn放在filter之前,跟之后没有区别,因为没有配置publishOn时,subscribeOn作用于所有,包括filter

window scheduler

还可以给window方法设定线程池

   @Testpublic void testWindowScheduler() throws InterruptedException {Scheduler windowScheduler = Schedulers.newSingle("window-thread");Flux.defer(() -> {LOGGER.info("defer thread:[{}]",Thread.currentThread().getName());return Flux.range(1,4);}).delayElements(Duration.ofMillis(200)) //默认会创建parallel线程,作用于subscribe线程.windowTimeout(1, Duration.ofMillis(100), windowScheduler).onErrorReturn(Flux.<Integer>just(-1)).flatMap(e -> {return e.map(item -> item*10);}).subscribe(e -> {LOGGER.info("subscribe thread:[{}],data :{}",Thread.currentThread().getName(),e);});Thread.sleep(10*1000);}
复制代码

输出

14:15:28.523 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
14:15:28.701 [main] INFO com.example.demo.SchedulerTest - defer thread:[main]
14:15:28.961 [parallel-1] INFO com.example.demo.SchedulerTest - subscribe thread:[parallel-1],data :10
14:15:29.167 [window-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[window-thread-1],data :20
14:15:29.370 [window-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[window-thread-1],data :30
14:15:29.573 [parallel-4] INFO com.example.demo.SchedulerTest - subscribe thread:[parallel-4],data :40
复制代码

注意delayElements方法默认给subscriber创建了parallel线程 timeout(),skip()等方法也默认会创建线程

scheduleGroup

前面在publishOn以及subscribeOn使用的都是Schedulers.newSingle,也可以使用多个线程组成的group,比如

Scheduler parallelGroup = Schedulers.newParallel("parallel-group", 8);
复制代码

也可以使用elastic类型,比较适合IO类型的操作

   /*** {@link Scheduler} that dynamically creates ExecutorService-based Workers and caches* the thread pools, reusing them once the Workers have been shut down.* <p>* The maximum number of created thread pools is unbounded.* <p>* The default time-to-live for unused thread pools is 60 seconds, use the appropriate* factory to push a different value.* <p>* This scheduler is not restartable.** @param name Thread prefix** @return a new {@link Scheduler} that hosts a fixed pool of single-threaded* ExecutorService-based workers and is suited for parallel work*/public static Scheduler newElastic(String name) {return newElastic(name, ElasticScheduler.DEFAULT_TTL_SECONDS);}
复制代码

实例

  @Testpublic void testElasticGroup() throws InterruptedException {Scheduler elastic = Schedulers.newElastic("elastic-group");Flux.defer(() -> {LOGGER.info("defer thread:[{}]",Thread.currentThread().getName());return Flux.range(1,4);}).filter(e -> {LOGGER.info("filter thread:[{}]",Thread.currentThread().getName());return e % 2 == 0;}).publishOn(elastic).map(e -> {LOGGER.info("map thread:[{}]",Thread.currentThread().getName());return e * 10;}).subscribeOn(elastic).subscribe(e -> {LOGGER.info("subscribe thread:[{}],data :{}",Thread.currentThread().getName(),e);});Thread.sleep(10*1000);}
复制代码

输出

13:58:37.356 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework
13:58:37.514 [elastic-group-2] INFO com.example.demo.SchedulerTest - defer thread:[elastic-group-2]
13:58:37.520 [elastic-group-2] INFO com.example.demo.SchedulerTest - filter thread:[elastic-group-2]
13:58:37.520 [elastic-group-2] INFO com.example.demo.SchedulerTest - filter thread:[elastic-group-2]
13:58:37.520 [elastic-group-2] INFO com.example.demo.SchedulerTest - filter thread:[elastic-group-2]
13:58:37.520 [elastic-group-2] INFO com.example.demo.SchedulerTest - filter thread:[elastic-group-2]
13:58:37.520 [elastic-group-3] INFO com.example.demo.SchedulerTest - map thread:[elastic-group-3]
13:58:37.520 [elastic-group-3] INFO com.example.demo.SchedulerTest - subscribe thread:[elastic-group-3],data :20
13:58:37.521 [elastic-group-3] INFO com.example.demo.SchedulerTest - map thread:[elastic-group-3]
13:58:37.521 [elastic-group-3] INFO com.example.demo.SchedulerTest - subscribe thread:[elastic-group-3],data :40
复制代码

小结

  • 命名 这个publishOn及subscribeOn方法名有点晦涩,更直白一点相当于subscriberThreadPools以及publisherThreadPools。
  • publishOn与operations的位置

在publishOn之后的filter或map等将使用publishOn配置的线程;之前的话,使用的是main线程或subscribeOn配置的线程

  • subscribeOn

在没有配置publishOn,只配置subscribeOn的话,则作用所有

  • 方法内置线程 delayElements(),timeout(),skip()内置会使用额外的线程

doc

  • schedulers

聊聊reactive streams的schedulers 1相关推荐

  1. 聊聊reactive streams的schedulers

    序 本文主要研究一下reactive streams的schedulers 背景 默认情况下Mono以及Flux都在主线程上运行,有时候可能会阻塞主线程,可以通过设定schedulers让其在其他线程 ...

  2. 聊聊reactive streams的processors

    序 本文主要研究一下reactive streams的processors processors分类 processors既是Publisher也是Subscriber.在project reacto ...

  3. Reactive Streams

    2019独角兽企业重金招聘Python工程师标准>>> Reactive Streams is an initiative to provide a standard for asy ...

  4. Reactive Streams规范及常见库

    一.什么是Reactive Streams Reactive Streams is an initiative to provide a standard for asynchronous strea ...

  5. JDK11的新特性:HTTP API和reactive streams

    文章目录 简介 怎么在java中使用reactive streams POST请求的例子 总结 简介 在JDK11的新特性:新的HTTP API中,我们介绍了通过新的HTTP API,我们可以发送同步 ...

  6. 什么是Reactive Streams in Java 译

    数式编程对于Reactive Programming很重要,但我不会在这篇文章中深入探讨函数式编程. 在这篇文章中,我想看看Java中的整体Reactive发展环境. Reactive Program ...

  7. JVM平台上的响应式流(Reactive Streams)规范

    Reactive Streams 响应式流是一个倡议,用来为具有非阻塞后压的异步流处理提供一个标准.大家努力的目标集中在运行时环境(JVM和JavaScript)和网络协议上. 注:响应式流其实就是一 ...

  8. Reactive Streams规范

    Reactive Streams的目的是提供一个带有 非阻塞背压 特征的 异步流处理 标准. 最新的发布版本如下 <dependency><groupId>org.reacti ...

  9. reactive streams的Mono及Flux

    实例 Mono @Testpublic void testMonoBasic(){Mono.fromSupplier(() -> "Hello").subscribe(Sys ...

  10. FunDA(6)- Reactive Streams:Play with Iteratees、Enumerator and Enumeratees

    在上一节我们介绍了Iteratee.它的功能是消耗从一些数据源推送过来的数据元素,不同的数据消耗方式代表了不同功能的Iteratee.所谓的数据源就是我们这节要讨论的Enumerator.Enumer ...

最新文章

  1. C#中Request.servervariables参数
  2. 沃丰报告:物联网的未来
  3. java右移位_java 、移位操作方法
  4. html中的灵动标签,《帝国网站管理系统》一招鲜吃天遍天系列教程之 灵动标签使用...
  5. oracle交叉运算符,Oracle语句优化44个规则详解
  6. 用ICC编译MP3编码器LAME
  7. 深入浅出PHP amp; MySQL,深入浅出 PHP MySQL
  8. 计算机网络之广域网基础以及华为eNSP实验配置
  9. android底部抽屉库,Xamarin.Android之简单的抽屉布局
  10. error: skipping because parent directory has insecure permissions问题
  11. python步长为负数_Python步长应用步进为负数时遇到的问题,python步长,python分片步长为负...
  12. 什么邮箱的归档功能好用?
  13. Windows 如何查看主板的型号和厂家,以及支持的最大内存
  14. 能说一说 Kotlin 中 lateinit 和 lazy 的区别吗?
  15. SVN :one or more files are in a conflicted state 一个或多个文件处于冲突状态
  16. 手淘抓包、 x-sign的签名算法和api接口
  17. 【python篇】python基础之--基本问题记录
  18. Activiti6 流程模型图中文显示为方块□□
  19. http://www.51it.org/zz/zzjx1/zzjx15/200604/51559.html
  20. 基于流向算法的WSN覆盖优化

热门文章

  1. Call to your teacher(深度搜索)
  2. bzoj3669(NOI2014)魔法森林
  3. java.lang.IllegalArgumentException: name MUST NOT NULL! at org.nutz.dao.impl.NutDao.fetch
  4. IOS动画隐式,显式,翻页
  5. oracle存储过程中返回一个程序集
  6. win7如何不用点击用户名 直接自动登录桌面
  7. 【习题基础知识】输入与输出、简单循环(好多图贴上去好累...要看图的话我把word文档发给你)...
  8. invocation, 作者 Medwyn Goodall,女巫医 [搜索 invocation Medwyn Goodall]
  9. .Net中数据绑定控件应用小技巧
  10. Eclipse基础--java环境变量设置