webclient - 在 ParallelFlux 上使用线程睡眠时,它不等待睡眠线程完成并执行 onComplete() 函数
问题描述
在 Flux 上使用 Parallel 时,我正在使用线程睡眠停止线程一段时间,但问题是 Flux 没有等到线程睡眠时间并在订阅时在 onComplete 上执行。
列表 str = new ArrayList<>(); str.add("弹簧"); str.add("webflux"); str.add("示例");
AtomicInteger num = new AtomicInteger(); ParallelFlux<Object> names = Flux.fromIterable(str) .log() .parallel(2) .runOn(Schedulers.boundedElastic()) .map( s-> { if(s.equalsIgnoreCase("webflux")) { try { System.out.println("waiting..."); Thread.sleep(1000); System.out.println("done..."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return s+" "+num.incrementAndGet(); }); names.subscribe(s -> { System.out.println("value "+s+" thread : "+Thread.currentThread().getName()); });
输出:
19:35:24.870 [main] INFO reactor.Flux.Iterable.1 - | onSubscribe([Synchronous Fuseable] FluxIterable.IterableSubscription)
19:35:24.896 [main] INFO reactor.Flux.Iterable.1 - | request(256)
19:35:24.897 [main] INFO reactor.Flux.Iterable.1 - | onNext(spring)
19:35:24.898 [main] INFO reactor.Flux.Iterable.1 - | onNext(webflux)
19:35:24.898 [main] INFO reactor.Flux.Iterable.1 - | onNext(example)
waiting...
value spring 1 thread : boundedElastic-1
value example 2 thread : boundedElastic-1
19:35:24.899 [main] INFO reactor.Flux.Iterable.1 - | onComplete()
解决方案
推荐阅读
- angular - Angular HttpClient POST 发送请求有效负载而不是表单数据
- javascript - 在html中的子元素内更改父属性
- sql-server - 在插入之前检查列是否包含值 T-SQL
- java - 单独的集合类对象
- javascript - 如何将文件发布到php服务器文件夹
- javascript - 将 Javascript 警报转换为工具提示
- javascript - 悬停时的导航滑动线 - 当没有项目是“当前”或“选定”时
- dart - 如何从异步方法返回?
- apache-flink - 用于滚动聚合的 GlobalWindow 的替代品
- javascript - 承诺异步功能