Java reactor:不适当的阻塞方法调用
Java reactor: Inappropriate blocking method call
我将其添加到代码中的两个位置
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)
哪些地方不应该阻塞线程?我知道我们必须避免阻塞调用,但我正在进行从反应式到非反应式的迁移,需要将此阻塞作为临时解决方法。
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.single
和 Schedulers.parallel
是此处相关的反应器调度程序。)
在另一个线程上进行阻塞可能没问题,只要您知道正在发生这种情况并了解这样做的后果。您通常会在迁移中故意阻塞的地方,例如在单独的线程池中(您专门为这些阻塞任务指定的线程池),或者内置反应堆 Schedulers.elastic
或 Schedulers.boundedElastic
池,专为此类目的而设计。
我将其添加到代码中的两个位置
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)
哪些地方不应该阻塞线程?我知道我们必须避免阻塞调用,但我正在进行从反应式到非反应式的迁移,需要将此阻塞作为临时解决方法。
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.single
和 Schedulers.parallel
是此处相关的反应器调度程序。)
在另一个线程上进行阻塞可能没问题,只要您知道正在发生这种情况并了解这样做的后果。您通常会在迁移中故意阻塞的地方,例如在单独的线程池中(您专门为这些阻塞任务指定的线程池),或者内置反应堆 Schedulers.elastic
或 Schedulers.boundedElastic
池,专为此类目的而设计。