同时使用多个线程中的同一FluxSink是否安全? [英] Is it safe to use the same FluxSink from multiple threads concurrently

查看:91
本文介绍了同时使用多个线程中的同一FluxSink是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 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.

推荐答案

是的, 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆