我将它添加到代码中的两个位置
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.single
和 Schedulers.parallel
是此处相关的 react 器调度程序。)
在另一个线程上阻塞可能是可以的,只要您知道正在发生这种情况并理解这样做的后果。您通常会在迁移中故意阻塞的地方,例如在单独的线程池中(您专门为这些阻塞任务指定的线程池),或者内置的 react 堆 Schedulers.elastic
或 Schedulers.boundedElastic
池,专为此类目的而设计。
关于Java react 堆 : Inappropriate blocking method call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59580211/