简体   繁体   English

在Spring上下文中从异步方法中调用bean的方法是否真的是并发的?

[英]Will calling a method of a bean in the spring context from an async method truly be concurrent?

I have a controller called importController. 我有一个名为importController的控制器。 importController has a method of return type void that calls an asynchronous method for a list of objects passed over the wire. importController有一个返回类型为void的方法,该方法为通过电线传递的对象列表调用异步方法。

The asynchronous methods logic is backed by a method in another object in the spring context. 异步方法逻辑由spring上下文中另一个对象中的方法支持。 As I understand it, all beans in the spring context are singleton by default. 据我了解,默认情况下,spring上下文中的所有bean都是单例的。

Will the asynchronous method calls really run in tandem or will they be bottle necked by the call to the bean? 异步方法调用会真正串联运行还是会因对bean的调用而成为瓶颈?

Thanks in advance for your help, Dylan 预先感谢您的帮助,迪伦

It doesn't matter if the bean is a singleton as long as it's method is not synchronized 只要bean的方法不synchronized是否为单例都没有关系

That is to say, if you have a singleton with a public String foo() method and it's called several times in different threads then foo's execution will be concurrent. 也就是说,如果您有一个带有public String foo()方法的单例,并且在不同线程中多次调用了该单例,则foo的执行将是并发的。

However, if foo is synchronized within the singleton bean like this : synchronized public String foo() then the threads will "take turns" (not exactly but you get my point) to run the foo method. 但是,如果foo在单例bean中是这样synchronized public String foo()synchronized public String foo()则线程将“轮流”运行(不完全是,但我明白了)来运行foo方法。

Hope this helps 希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM