gpt4 book ai didi

java - Schedulers.newElastic 和 Schedulers.elastic 方法有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:41 28 4
gpt4 key购买 nike

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

有很多选项可以使用 elastic、parallel 和 newElastic 来启动 Schedular。

这是我使用的代码:

    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();

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

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.

这是他们两个的日志:

------ 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()

两者有什么区别?

最佳答案

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

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

在此处检查 Scheduler 类的文档:https://projectreactor.io/docs/core/release/api/

关于java - Schedulers.newElastic 和 Schedulers.elastic 方法有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48095075/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com