我在 C# 多线程程序中遇到了不良行为。我的一些静态成员在其他线程中丢失了它们的值,而相同声明类型的一些静态值不会丢失它们的值。
public class Context {
  public Int32 ID { get; set; }
  public String Name { get; set; }
  public Context(Int32 NewID, String NewName){
      this.ID = NewID;
      this.Name = NewName;
  }
}
public class Root {
    public static Context MyContext;
    public static Processor MyProcessor;
   public Root(){
     Root.MyContext = new Context(1,"Hal");
     if(Root.MyContext.ID == null || Root.MyContext.ID != 1){
         throw new Exception("Its bogus!") // Never gets thrown
     }
     if(Root.MyContext.Name == null || Root.MyContext.Name != "Hal"){
         throw new Exception("It's VERY Bogus!"); // Never gets thrown
     } 
     Root.MyProcessor = new MyProcessor();
     Root.MyProcessor.Start();
   }
}
public class Processor {
   public Processor() {
   }
   public void Start(){
      Thread T= new Thread (()=> {
          if(Root.MyContext.Name == null || Root.MyContext.Name != "Hal"){
                throw new Exception("Ive lost my value!"); // Never gets Thrown
          }
          if(Root.MyContext.ID == null){
              throw new Exception("Ive lost my value!"); // Always gets thrown
          }
      });
   }
}
这是使用某些类型的静态成员时的线程突变问题吗?