我有一个从多个线程访问的C#静态类.两个问题:
使用来自不同线程的静态类:
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
Task.Run(() =>
{
string name = MyStaticClass.GetValue(9555);
//...
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
静态类的选项1:
public static class MyStaticClass
{
private static MyClass _myClass = new MyClass();
public static string GetValue(int key)
{
return _myClass.GetValue(key);
}
}
Run Code Online (Sandbox Code Playgroud)
静态类的选项2:
public static class MyStaticClass
{
private static MyClass _myClass;
private static object _lockObj = new object();
static MyStaticClass()
{
InitMyClass();
}
private …Run Code Online (Sandbox Code Playgroud)