问题描述
我想问一下Spring @Async带注释的方法如何处理作为参数传递的对象.异步方法的执行将创建另一个线程.在这种情况下,调用者和新线程都具有对作为参数传入的对象的引用.这意味着两个线程都能够变异相同的对象.这可能会导致错误的结果.我想问一下Spring如何处理这个问题.也许以某种方式复制/克隆了作为参数传入的对象?
I wanted to ask how Spring @Async annotated methods do handle with objects passed as a arguments.The async method execution will create another thread. In this case the caller and the new thread have both a reference to the objects passed in as arguments. That means both threads are able to mutate the same objects. This may lead to false results. I wanted to ask how Spring is handling this. Maybe the objects passed in as arguments are somehow copied/cloned?
先谢谢您了:)
推荐答案
Spring甚至没有尝试处理它.作为方法的开发人员,您有责任实现该方法,以便在并发环境中正常工作.访问共享数据是并发,其解决方案.
Spring is not even try to handle it. It is your responsibility as a developer to implement the method so it works correctly in concurrent environment. Accessing shared data is a general problem (or I would say implication) of concurrency which has its solutions.
这篇关于传递给异步方法的参数的Spring @Asnyc方法调用和线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!