Spring bean 如何处理并发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13776728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:34:59  来源:igfitidea点击:

How does Spring bean Handle concurrency

springthread-safetyjavabeans

提问by Rajeev Akotkar

My web application uses Spring IOC. So all my spring beans will be singletons by default. In case if two requests try to access two different methods of a single class (for example MySpringBeanis a class which has two methods searchRecordand insertRecord) at the same time, both the requests will access the same spring bean concurrently.

我的 Web 应用程序使用 Spring IOC。所以我所有的 spring bean 默认都是单例的。如果两个请求试图同时访问同一个类的两个不同方法(例如MySpringBean一个有两个方法searchRecord和的类insertRecord),两个请求将同时访问同一个 spring bean。

How does the same spring bean be available to both the clients at the same time or is it going to be concurrency problem when both the requests will try to access two different methods but through the same spring bean. And since spring bean is a singleton so new instance can not be formed. In this case how is this going to work?

两个客户端如何同时使用同一个 spring bean,或者当两个请求都尝试访问两个不同的方法但通过同一个 spring bean 时,这是否会成为并发问题。由于 spring bean 是单例,因此无法形成新实例。在这种情况下,这将如何工作?

回答by Tomasz Nurkiewicz

You must first understand when concurrency can cause problems. If your Spring bean is stateless (it doesn't have any fields, all fields are finalor all of them are assigned only once), multiple threads can safely use the same bean, or even the same method.

您必须首先了解并发何时会导致问题。如果您的 Spring bean 是无状态的(它没有任何字段,所有字段final或所有字段都只分配一次),则多个线程可以安全地使用同一个 bean,甚至同一个方法。

See also:

也可以看看:

回答by hertzsprung

If the bean is a singleton, then Spring will give you the same instance in any thread. It's up to you to make that bean thread-safe. Since it's a singleton, you'd be best off making that class stateless.

如果 bean 是单例,那么 Spring 将在任何线程中为您提供相同的实例。使该 bean 成为线程安全的由您决定。由于它是一个单身人士,因此最好使该类无状态。

回答by Raghuram

As others have already suggested, Spring is going to provide the same instance to all the threads in case of "singleton" beans.

正如其他人已经建议的那样,在“单例”bean 的情况下,Spring 将为所有线程提供相同的实例。

What you need to understand is that threads do all the work in a system by executing the code while objects provide state and behavior (code). So it is indeed possible for multiple threads (requests in your case), to be concurrently running same methods in a singleton bean. You can either make such beans stateless as Tomasz suggested or otherwise make them "thread-safe".

您需要了解的是,线程通过执行代码来完成系统中的所有工作,而对象提供状态和行为(代码)。因此,多个线程(在您的情况下是请求)确实可以在单例 bean 中同时运行相同的方法。您可以按照 Tomasz 的建议使此类 bean 无状态,也可以使它们成为“线程安全的”。

回答by Mohamed Salih

Java singleton and spring singleton are different. Spring singleton scope will be available within context.

Java单例和spring单例是不同的。Spring 单例范围将在上下文中可用。

Java singleton scope will be in JVM class loader. Hence concurrent request possible only through spring contexts

Java 单例范围将在 JVM 类加载器中。因此并发请求只能通过 spring 上下文