是春天异步传递对象的线程安全的previous更新 [英] is spring async pass object thread safe to previous update
问题描述
我们使用Spring框架的异步,样本code如下,如果实体对象是可变的,并且已经更新之前调用put,将JVM保证把会看到改变?
@Async(daoExecutor)
@Repository
公共类GenericDaoImpl实现GenericDao {
公共CompletableFuture<实体GT;把(实体E){
}
}
是
@Async
通过包装在实际的方法调用或多或少实施可赎回
并提交该调用到一个的ThreadPoolExecutor
。这是提交通过的BlockingQueue
完成。由JDK(其中Spring使用)提供的默认的BlockingQueue
的实现都是线程安全的。添加到队列中的发生-之前的从队列中取。
所以,你打电话之前所做的任何更改把
(并添加到队列)将在可见的把
(这从队列服用后发生)。
we are using spring async framework, sample code as below, if Entity object is mutable and has been updated BEFORE the call to put, will JVM guarantee that put will see the change?
@Async("daoExecutor")
@Repository
public class GenericDaoImpl implements GenericDao {
public CompletableFuture<Entity> put(Entity e) {
}
}
Yes.
@Async
is more or less implemented by wrapping the actual method invocation in a Callable
and submitting that callable to a ThreadPoolExecutor
. That submission is done through a BlockingQueue
. The default BlockingQueue
implementations provided by the JDK (which Spring uses) are all thread safe. Adding to the queue happens-before taking from the queue.
So any changes you make before calling put
(and adding to the queue) will be visible in the put
(which happens after taking from the queue).
这篇关于是春天异步传递对象的线程安全的previous更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!