Schedulers.newElastic和Schedulers.elastic方法之间有什么区别?

时间:2022-02-04 02:12:15

I am working on Flux and Mono and using them in multi threaded environment and using the Schedular which provide the worker thread.

我正在研究Flux和Mono,并在多线程环境中使用它们并使用提供工作线程的Schedular。

There are many options to start the Schedular using elastic, parallel and newElastic.

使用elastic,parallel和newElastic启动Schedular有很多选项。

Here is the code which i used:

这是我使用的代码:

    System.out.println("------ elastic ---------  ");
    Flux.range(1, 10)
      .map(i -> i / 2)
      .publishOn(Schedulers.elastic()).log()
      .blockLast();

    System.out.println("------ new elastic ---------  ");
    Flux.range(1, 10)
      .map(i -> i / 2).log()
      .publishOn(Schedulers.newElastic("my")).log()
      .blockLast();

and both of them have the same documentation:

并且它们都有相同的文档:

Scheduler that dynamically creates ExecutorService-based Workers and caches the thread pools, reusing them once the Workers have been shut down.

调度程序,动态创建基于ExecutorService的工作程序并缓存线程池,一旦工作程序关闭就重新使用它们。

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.

未使用的线程池的默认生存时间为60秒,使用适当的工厂来推送不同的值。

This scheduler is not restartable.

此调度程序无法重新启动。

and here is the logs for both of them:

这是他们两个的日志:

------ elastic ---------  
[ INFO] (main) | onSubscribe([Fuseable] FluxPublishOn.PublishOnSubscriber)
[ INFO] (main) | request(unbounded)
[ INFO] (elastic-2) | onNext(0)
[ INFO] (elastic-2) | onNext(1)
[ INFO] (elastic-2) | onNext(1)
[ INFO] (elastic-2) | onNext(2)
[ INFO] (elastic-2) | onNext(2)
[ INFO] (elastic-2) | onNext(3)
[ INFO] (elastic-2) | onNext(3)
[ INFO] (elastic-2) | onNext(4)
[ INFO] (elastic-2) | onNext(4)
[ INFO] (elastic-2) | onNext(5)
[ INFO] (elastic-2) | onComplete()
------ new elastic ---------  
[ INFO] (main) | onSubscribe([Fuseable] FluxMapFuseable.MapFuseableSubscriber)
[ INFO] (main) | onSubscribe([Fuseable] FluxPublishOn.PublishOnSubscriber)
[ INFO] (main) | request(unbounded)
[ INFO] (main) | request(256)
[ INFO] (main) | onNext(0)
[ INFO] (main) | onNext(1)
[ INFO] (my-4) | onNext(0)
[ INFO] (main) | onNext(1)
[ INFO] (my-4) | onNext(1)
[ INFO] (main) | onNext(2)
[ INFO] (my-4) | onNext(1)
[ INFO] (my-4) | onNext(2)
[ INFO] (main) | onNext(2)
[ INFO] (main) | onNext(3)
[ INFO] (my-4) | onNext(2)
[ INFO] (main) | onNext(3)
[ INFO] (my-4) | onNext(3)
[ INFO] (my-4) | onNext(3)
[ INFO] (main) | onNext(4)
[ INFO] (my-4) | onNext(4)
[ INFO] (main) | onNext(4)
[ INFO] (main) | onNext(5)
[ INFO] (my-4) | onNext(4)
[ INFO] (main) | onComplete()
[ INFO] (my-4) | onNext(5)
[ INFO] (my-4) | onComplete()

What is the difference between the two?

两者有什么区别?

1 个解决方案

#1


2  

The elastic() function returns a shared scheduler instance. This means that multiple calls to this function will return the same scheduler.

elastic()函数返回共享的调度程序实例。这意味着对此函数的多次调用将返回相同的调度程序。

The functions prefixed with new will always create a new scheduler instance.

以new为前缀的函数将始终创建新的调度程序实例。

Check the docs for the Schedulers class here: https://projectreactor.io/docs/core/release/api/

在这里查看调度程序类的文档:https://projectreactor.io/docs/core/release/api/

#1


2  

The elastic() function returns a shared scheduler instance. This means that multiple calls to this function will return the same scheduler.

elastic()函数返回共享的调度程序实例。这意味着对此函数的多次调用将返回相同的调度程序。

The functions prefixed with new will always create a new scheduler instance.

以new为前缀的函数将始终创建新的调度程序实例。

Check the docs for the Schedulers class here: https://projectreactor.io/docs/core/release/api/

在这里查看调度程序类的文档:https://projectreactor.io/docs/core/release/api/