同时使用多个线程中的同一FluxSink是否安全? [英] Is it safe to use the same FluxSink from multiple threads concurrently
问题描述
我知道 Publisher
不能同时发布,但是如果我使用 Flux#create(FluxSink)
,我可以安全地呼叫 FluxSink#next
并发?
I know a Publisher
must not publish concurrently, but if I use Flux#create(FluxSink)
, can I safely call FluxSink#next
concurrently?
换句话说,即使同时调用 FluxSink#next
,Spring是否具有内部魔术来确保事件的正确串行发布?
In other words, does Spring have internal magic that ensures proper serial publishing of events even if FluxSink#next
is called concurrently?
public class FluxTest {
private final Map<String, FluxSink<Item>> sinks = new ConcurrentHashMap<>();
// Store a new sink for the given ID
public void start(String id) {
Flux.create(sink -> sinks.put(id, sink));
}
// Called from different threads
public void publish(String id, Item item) {
sinks.get(id).next(item); //<----------- Is this safe??
}
}
它对我来说听起来很像,例如此段在官方指南中表示上述内容确实是安全的,但我对自己的理解并不十分自信.
It sounds to me like this paragraph in the official guide indicates that the above is indeed safe, but I'm not very confident in my understanding.
create
是Flux的程序化创建的一种更高级形式,它适用于每轮多次排放,甚至来自多个线程.
create
is a more advanced form of programmatic creation of a Flux which is suitable for multiple emissions per round, even from multiple threads.
推荐答案
是的, SerializedSink
可以安全地在多个线程中用于下一个
调用
这篇关于同时使用多个线程中的同一FluxSink是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!