类级属性或变量是线程安全的

发布于 2024-10-04 08:45:59 字数 456 浏览 0 评论 0原文

我总是对这个特定的场景感到担忧。假设我的类看起来像这样

public class Person {
public Address Address{get;set;}
public string someMethod()
{}
}

我的问题是,我的开发人员同事告诉我,Address 类型的 Address 属性不是线程安全的。

从 Web 请求的角度来看,每个请求都在单独的线程上运行,并且每次 线程在我的业务对象或隐藏代码中处理以下行,例如

var p = new Person();

,它在堆上创建 Person 对象的新实例,因此请求线程可以访问该实例,除非我在应用程序中生成多个线程。

如果我错了,请向我解释为什么我错了,为什么公共财产(地址)不是线程安全的?

任何帮助将不胜感激。

谢谢。

I always had this specific scenario worry me for eons. Let's say my class looks like this

public class Person {
public Address Address{get;set;}
public string someMethod()
{}
}

My question is, I was told by my fellow developers that the Address propery of type Address, is not thread safe.

From a web request perspective, every request is run on a separate thread and every time
the thread processes the following line in my business object or code behind, example

var p = new Person();

it creates a new instance of Person object on heap and so the instance is accessed by the requesting thread, unless and otherwise I spawn multiple threads in my application.

If I am wrong, please explain to me why I am wrong and why the public property (Address) is not thread safe?

Any help will be much appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空心空情空意 2024-10-11 08:45:59

如果对 Person 实例的引用在多个线程之间共享,则多个线程可能会更改 Address 从而导致竞争条件。但是,除非您将该引用保存在静态字段或会话(某种全局可访问的位置)中,否则您无需担心。

如果您在代码中创建对对象的引用,如上面所示 (var p = new Person();),那么您是完全线程安全的,因为其他线程将无法访问对对象的引用这些对象无需诉诸令人讨厌和恶意的伎俩。

If the reference to your Person instance is shared among multiple threads then multiple threads could potentially change Address causing a race condition. However unless you are holding that reference in a static field or in Session (some sort of globally accessible place) then you don't have anything to be worried about.

If you are creating references to objects in your code like you have show above (var p = new Person();) then you are perfectly thread safe as other threads will not be able to access the reference to these objects without resorting to nasty and malicious tricks.

错爱 2024-10-11 08:45:59

您的属性不是线程安全的,因为您没有锁定来防止对属性的多次写入互相干扰。

但是,在您不在多个线程之间共享类实例的情况下,该属性不需要是线程安全的。

在多个线程之间共享的对象,其中每个线程都可以更改对象的状态,那么所有状态更改都需要受到保护,以便一次只有一个线程可以修改该对象。

Your property is not thread safe, because you have no locking to prevent multiple writes to the property stepping on each others toes.

However, in your scenario where you are not sharing an instance of your class between multiple threads, the property doesn't need to be thread safe.

Objects that are shared between multiple threads, where each thread can change the state of the object, then all state changes need to be protected so that only one thread at a time can modify the object.

夜空下最亮的亮点 2024-10-11 08:45:59

你应该对此没问题,但是有一些事情我会担心......

如果你的 Person 对象要被修改或持有一些一次性资源,你可能会发现其中一个线程将无法读取这个多变的。为了防止这种情况,您需要在读/写对象之前锁定该对象,以确保它不会被其他线程践踏。最简单的方法是使用 lock{} 构造。

You should be fine with this, however there are a few things I'd worry about...

If your Person object was to be modified or held some disposable resources, you could potentially find that one of the threads will be unable to read this variable. To prevent this, you will need to lock the object before read/writing it to ensure it won't be trampled on by other threads. The easiest way is by using the lock{} construct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文