博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
聊聊reactive streams的schedulers
阅读量:6515 次
发布时间:2019-06-24

本文共 12015 字,大约阅读时间需要 40 分钟。

  hot3.png

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

背景

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

原始输出

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

11:26:10.668 [main] DEBUG reactor.util.Loggers$LoggerFactory - Using Slf4j logging framework11: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 :211: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配置线程)

@Test    public 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 framework11: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 :211:31:23.881 [publisher-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[publisher-thread-1],data :4

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

subscribeOn(给publisher配置线程)

@Test    public 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 framework11: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 :211: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

@Test    public 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 framework11: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 :211: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

@Test    public 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 framework13: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 :213: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方法设定线程池

@Test    public 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.
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 framework14: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 :1014:15:29.167 [window-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[window-thread-1],data :2014:15:29.370 [window-thread-1] INFO com.example.demo.SchedulerTest - subscribe thread:[window-thread-1],data :3014: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.	 * 

* The maximum number of created thread pools is unbounded. *

* The default time-to-live for unused thread pools is 60 seconds, use the appropriate * factory to push a different value. *

* 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); }

实例

@Test    public 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 framework13: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 :2013: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

转载于:https://my.oschina.net/go4it/blog/1607342

你可能感兴趣的文章
libcurl以get方式请求服务器端文件
查看>>
OpenJudge 2786 Pell数列
查看>>
mysql 游标循环,嵌套游标循环
查看>>
win7 蛋疼的时间格式转化
查看>>
C++中二维数组的动态创建与处理
查看>>
SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)
查看>>
SpringInAction--Bean参数的自动注入
查看>>
素数筛
查看>>
centos /linux 修改目录或文件权限
查看>>
leetcode--
查看>>
访问者模式
查看>>
异步函数
查看>>
Openstack的vnc界面定制
查看>>
软考 2018年下半年卷 错题知识点记录
查看>>
仿网易邮箱5.0版UI
查看>>
winsow xp不能安装软件, 提示"中断" 是因为设置了 软件限制策略
查看>>
as3调用外部应用程序 as调用外部exe文件as3调用bat文件 未测试
查看>>
jQuery清空标签内容--防止内存泄露
查看>>
关于 HandlerMethodArgumentResolver 类 以及 WebArgumentResolver 类 自定义解析参数
查看>>
30个php操作redis常用方法代码例子
查看>>