Spring对象是线程安全的吗? [英] Are Spring objects thread safe?

查看:426
本文介绍了Spring对象是线程安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring对象线程安全吗?如果没有,如何使它们线程安全?

Are Spring objects thread safe? If not, how to make them thread safe?

推荐答案

这是两个不相关的问题:

These are two unrelated questions:

不.

Spring有不同的bean 范围(例如Prototype,Singleton等),但是所有这些范围强制都在创建bean时 进行.例如,每次注入"一个原型"范围的bean都会被创建,而一个单个"范围的bean将被创建一次并在应用程序上下文中共享.还有其他范围,但是它们只是定义了何时创建新实例的时间范围(例如范围").

Spring has different bean scopes (e.g. Prototype, Singleton, etc.) but all these scopes enforce is when the bean is created. For example a "prototype" scoped bean will be created each time this bean is "injected", whereas a "singleton" scoped bean will be created once and shared within the application context. There are other scopes but they just define a time span (e.g. a "scope") of when a new instance will be created.

以上内容与线程安全无关,因为如果多个线程可以访问一个bean(无论范围如何),那么它仅取决于该bean的 design 是或不是线程安全"的.

The above has little, if anything to do with being thread safe, since if several threads have access to a bean (no matter the scope), it would only depend on the design of that bean to be or not to be "thread safe".

我之所以说很少,如果有的话"是因为它可能取决于您要解决的问题.例如,如果您担心两个或更多个HTTP请求是否可能对同一个bean造成问题,则有一个,它将为每个HTTP请求创建一个新的bean实例,因此您可以将特定" bean视为在多个HTTP请求的上下文中是安全的".但是 仍然不是真正的线程安全线程,因为如果多个线程在同一 HTTP请求中使用此bean,它将返回到bean设计( your bean支持类的设计.

The reason I said "little, if anything" is because it might depend on the problem you are trying to solve. For example if you are concerned whether 2 or more HTTP requests may create a problem for the same bean, there is a "request" scope that will create a new instance of a bean for each HTTP request, hence you can "think" of a particular bean as being "safe" in the context of multiple HTTP requests. But it is still not truly thread safe by Spring since if several threads use this bean within the same HTTP request, it goes back to a bean design (your design of a bean backing class).

有几种方法,可能在这里列出太久了,但是这里有一些示例:

There are several ways, probably too long to list here but here are a few examples:

  • Design your beans immutable: for example have no setters and only use constructor arguments to create a bean. There are other ways, such as Builder pattern, etc..

设计您的bean 无状态:例如,一个可以做某事的bean可以只是一个函数(或多个).在大多数情况下,此bean可以并且应该是无状态的,这意味着它没有任何状态,只有具有每次(每次调用)提供的函数参数的事物

Design your beans stateless: for example a bean that does something can be just a function (or several). This bean in most cases can and should be stateless, which means it does not have any state, it only does things with function arguments you provide each time (on each invocation)

设计您的bean 永久 :这是一个不可变"的特例,但具有一些非常好的属性.通常在函数式编程中使用,其中Spring(至少到目前为止)没有在命令式世界中有用,但是我已经将它们用于Scala/Spring项目中.

Design your beans persistent: which is a special case of "immutable", but has some very nice properties. Usually is used in functional programming, where Spring (at least yet) not as useful as in imperative world, but I have used them with Scala/Spring projects.

设计带有锁的bean [不得已]:除非您正在使用级别的库,否则我建议不要这样做.原因是我们(人类)对锁的想法不好.就像我们成长和培养的方式一样.一切并行进行,而无需我们暂停下雨,让我打伞".但是,当您同时谈论多个事情"时,计算机都是关于锁的,因此我们中的某些人(特殊人士)正在公平地分享自己的知识并基于这些锁实现库.其他大多数人都可以使用这些库,而不必担心并发.

Design your beans with locks [last resort]: I would recommend against this unless you are working on a lower level library. The reason is we (humans) are not good thinking in terms of locks. Just the way we are raised and nurtured. Everything happens in parallel without us needing to "put that rain on pause, let me get an umbrella". Computers however are all about locks when you are talking "multiple things at the same time", hence there are some of us (exceptional people) who are doing their fair share and implementing libraries based on these locks. Most of other humans can just use these libraries and worry not about concurrency.

这篇关于Spring对象是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆