小编Day*_*ayo的帖子

Schedulers.boundedElastic 似乎使用相同的线程进行处理

通过查看 API,我的理解是使用 Schedulers.boundedElastic() 或 Schedulers.newBoundedElastic(3, 10, "MyThreadGroup"); 等变体;或 Schedulers.fromExecutor(executor) 允许在多个线程中处理 IO 操作。

但是使用以下示例代码进行的模拟似乎表明单个线程/同一线程正在 flatMap 中执行工作

Flux.range(0, 100)
                .flatMap(i -> {
                    try {
                        // IO operation
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Mapping for " + i + " is done by thread " + Thread.currentThread().getName());
                    return Flux.just(i);
                })
                .subscribeOn(Schedulers.boundedElastic())
                .subscribe();

Thread.sleep(10000); // main thread

//This yields the following

Mapping for 0 is done by thread boundedElastic-1
Mapping for 1 is done by thread boundedElastic-1
Mapping for 2 is …
Run Code Online (Sandbox Code Playgroud)

multithreading project-reactor spring-webflux

4
推荐指数
1
解决办法
7655
查看次数