[英]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.