Java react 堆 : Inappropriate blocking method call

标签 java spring-webflux project-reactor reactive-streams

我将它添加到代码中的两个位置

Flux.empty()
    .collectList()
    .block();

在一种情况下,IntelliJ 会突出显示 .block() 并显示错误消息 Inappropriate blocking method call。别的地方还行。

Settings -> Inspections 中,我看到这可能是因为在这个片段中线程不应该被阻塞。

Reports thread-blocking method calls found in a code fragment where a thread should not be blocked (e.g. Reactive frameworks, Kotlin coroutines)

哪些地方不应该阻塞线程?我知道我们必须避免阻塞调用,但我正在进行从响应式(Reactive)到非响应式(Reactive)的迁移,需要将此阻塞作为临时解决方法。

最佳答案

In what places thread should not be blocked? I know that we have to avoid blocking calls but I'm doing a migration from reactive to non-reactive and need this blocking as a temporary workaround.

绝对禁止使用 Netty 事件循环线程,因为有意地只有少数这些线程,并且它们被设计为始终忙碌(从不阻塞)以实现最大吞吐量。这可能是 IntelliJ 所提示的。 (Schedulers.singleSchedulers.parallel 是此处相关的 react 器调度程序。)

在另一个线程上阻塞可能是可以的,只要您知道正在发生这种情况并理解这样做的后果。您通常会在迁移中故意阻塞的地方,例如在单独的线程池中(您专门为这些阻塞任务指定的线程池),或者内置的 react 堆 Schedulers.elasticSchedulers.boundedElastic 池,专为此类目的而设计。

关于Java react 堆 : Inappropriate blocking method call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580211/

相关文章:

java - 具有生成的属性和排序的 Vaadin 网格

java - 如何修复 Pattern.compile(regex, Pattern.CASE_INSENSITIVE) 上的 Sonar 关键问题;

java - Spring云合约和webflux路由

java - 如何增加Spring WebClient并发请求限制?

java - Java Reactor 中嵌套的 flatMap 有什么好的习惯用法?

java - libGDX 操作 : when they will execute

Java pong 游戏 Graphics 或 geom 2D

functional-programming - 如何以非阻塞方式解析 Spring 5 WebClient 响应?

json - 如何从 SpringBoot Controller 中的 Flux<String> 获取 JSON 格式?

java - react 器WebFlux : help to understand how to work flatMap()