从多个线程同时使用同一个 FluxSink 是否安全
Is it safe to use the same FluxSink from multiple threads concurrently
我知道 Publisher
不能并发发布,但是如果我使用 Flux#create(FluxSink)
,我可以安全地并发调用 FluxSink#next
吗?
换句话说,即使 FluxSink#next
被并发调用,Spring 是否具有确保正确串行发布事件的内在魔力?
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??
}
}
我听起来像官方指南中的this paragraph说明上面确实是安全的,但我对我的理解不太自信
create
is a more advanced form of programmatic creation of a Flux which is suitable for multiple emissions per round, even from multiple threads.
是的,Flux.create
produces a SerializedSink
可以安全地从多个线程使用 next
调用
我知道 Publisher
不能并发发布,但是如果我使用 Flux#create(FluxSink)
,我可以安全地并发调用 FluxSink#next
吗?
换句话说,即使 FluxSink#next
被并发调用,Spring 是否具有确保正确串行发布事件的内在魔力?
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??
}
}
我听起来像官方指南中的this paragraph说明上面确实是安全的,但我对我的理解不太自信
create
is a more advanced form of programmatic creation of a Flux which is suitable for multiple emissions per round, even from multiple threads.
是的,Flux.create
produces a SerializedSink
可以安全地从多个线程使用 next
调用