简体   繁体   English

我们可以以线程安全的方式从方法中返回 Function 吗?

[英]Can we return Function from a method in a thread-safe way?

private Function<ServiceBean, Mono<SomeResponse>> someFunction(SomeRequest someRequest) {
    return serviceBean -> serviceBean.doSomething(someRequest)
            .next();
}

Is the above method safe?以上方法安全吗?

If I create, say 10 threads, with different type of SomeRequests and call this method at the same time, is it safe to assume there is threadsafety?如果我创建 10 个线程,使用不同类型的SomeRequests并同时调用此方法,假设存在线程安全是否安全?

Yes, this is thread-safe.是的,这是线程安全的。 But each time someFunction(..) is called, it'll create a new lambda.但是每次调用 someFunction(..) 时,它都会创建一个新的 lambda。 Even though lamdas are light weight objects, its not a good idea to create a Function like this.尽管 lamda 是轻量级对象,但创建这样的函数并不是一个好主意。 Its better to have a BiFunction declared at the class level.最好在类级别声明一个 BiFunction。

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

相关问题 ConcurrentHashMap put() 方法可以不是线程安全的吗? - Can ConcurrentHashMap put() method be not thread-safe? 如果所有集合属性都是线程安全的,那么我们可以说这个集合是线程安全的吗? - If all collection attributes are thread-safe , can we say that this collection is thread-safe? BoneCP:getConnection方法是线程安全的还是我们必须处理它? - BoneCP: Is the getConnection method thread-safe or we have to handle that? 一种快速线程安全的方法来标识源自超类的方法调用 - A fast thread-safe way to identify method calls originating from the superclass 此方法是否可以在线程安全且没有死锁的情况下工作 - Will this method work thread-safe and without Deadlocks Java HashSet 添加方法线程安全吗? - Is Java HashSet add method thread-safe? 包含循环的静态方法是否是线程安全的? - Is a static method containing loops thread-safe? ConcurrentSkipListMap将方法置于线程安全吗? - Is ConcurrentSkipListMap put method thread-safe? 是否可以使用带有两个AtomicInteger的线程安全方法? - Is a Thread-safe method with two AtomicIntegers possible? 登录后以线程安全的方式发出请求 - Logging in then making a request in a thread-safe way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM