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

一个人想着一个人 提交于 2021-01-24 11:36:07

问题


I know a Publisher must not publish concurrently, but if I use Flux#create(FluxSink), can I safely call FluxSink#next concurrently?

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 is a more advanced form of programmatic creation of a Flux which is suitable for multiple emissions per round, even from multiple threads.


回答1:


Yes, Flux.create produces a SerializedSink that is safe to use from multiple threads for next calls



来源:https://stackoverflow.com/questions/52558911/is-it-safe-to-use-the-same-fluxsink-from-multiple-threads-concurrently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!