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

查看:48
本文介绍了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,而将创建一次单个"作用域 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 的设计是否线程安全".

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".

我之所以说很少,如果有的话"是因为这可能取决于您要解决的问题.例如,如果您担心 2 个或更多 HTTP 请求是否会为同一个 bean 造成问题,则有一个 "request" scope 它将为每个 HTTP 请求创建一个 bean 的新实例,因此您可以认为"一个特定的 bean在多个 HTTP 请求的上下文中是安全的".但是它仍然不是真正的线程安全Spring,因为如果多个线程在同一个 HTTP 请求中使用这个 bean,它会回到 bean 设计(你的 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:

  • 设计您的 bean 不可变:例如,没有 setter,仅使用构造函数参数来创建 bean.还有其他方式,比如Builder模式等.

  • 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 persistent:这是一个不可变"的特例,但有一些非常好的属性.通常用于函数式编程,其中 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天全站免登陆