是静态方法的线程安全的 [英] Is static method thread safe

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

问题描述

如果我有一个静态的方法,将一个对象转换到另一个对象, 是这种方法的线程安全的C#?

If I have a static method to convert one object to another object, is this method thread safe in C#?

public AnotherDataClass Convert(MyDataClass target)
{
     AnotherDataClass val = new AnotherDataClass();
     // read infomration from target
     // put information into val;
     return val;
}

只是想使问题更加清晰......

Just want to make the question more clear....

在调用转换方法....我们可以假设目标是不会被修改。 由于转换方法只关心attrubite的目标

when invoke the convert method.... we can assume that target is not going be be modified. since the Convert method only interested in the "attrubite" of target

推荐答案

没有,事实并非如此。

的方法将是线程安全的,如果将​​b访问的数据将无法访问任何其他线程如果这个定义是正确的,则该方法不是线程安全的。

"A method would be thread safe if it would b accessing data that won't be accessible to any other thread" If this definition is correct then the method is not thread safe

原因

MyDataClass 似乎引用类型对我那么有一个机会,多线程可能会改变目标变量

MyDataClass seems reference type to me so there is a chance that multiple threads might be changing the target variable

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

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