【发布时间】:2010-07-28 09:53:39
【问题描述】:
最近我对在一个由数百个线程使用的类中使用静态方法产生了争议。 在我看来,“第一个”解决方案没有什么特别的好处,只是它更容易为班级客户使用,但有人告诉我这是非常糟糕的主意,我必须使用“第二个”解决方案。谁能给我清楚的解释为什么我错了? 非常感谢!
PS 假设字典是线程安全的。
class A
{
static Dictionary<int, string> m_dic = new Dictionary<int, string>();
public static string GetData1(int nKey)
{
return m_dic[nKey];
}
public string GetData2(int nKey)
{
return m_dic[nKey];
}
}
//these func are called from threads...
void ThreadFunc1()
{
print A.GetData1(1);
}
void ThreadFunc2()
{
A a = new A();
print a.GetData2(1);
}
问候, 狮子座
【问题讨论】:
标签: c# multithreading static