简体   繁体   English

同时从多个线程使用同一FluxSink是否安全?

[英]Is it safe to use the same FluxSink from multiple threads concurrently

I know a Publisher must not publish concurrently, but if I use Flux#create(FluxSink) , can I safely call FluxSink#next concurrently? 我知道Publisher一定不能同时发布,但是如果我使用Flux#create(FluxSink) ,可以安全地同时调用FluxSink#next吗?

In other words, does Spring have internal magic that ensures proper serial publishing of events even if FluxSink#next is called concurrently? 换句话说,即使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??
    }
}

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 is a more advanced form of programmatic creation of a Flux which is suitable for multiple emissions per round, even from multiple threads. create是Flux的程序化创建的一种更高级形式,它适合于每轮多次排放,甚至来自多个线程。

是的, Flux.create产生了一个SerializedSink ,可以安全地从多个线程中使用它进行next调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM