【发布时间】:2018-02-04 01:16:32
【问题描述】:
这是我的测试代码:
class Program
{
static void Main(string[] args)
{
new Thread(delegate() { runThread(); }).Start();
Console.WriteLine(Global.test);
Console.ReadKey();
}
private static void runThread()
{
Console.WriteLine("this is run thread");
Global.test = "this is test from thread";
Console.WriteLine(Global.test);
}
}
public class Global
{
public static string testV { get; set; }
}
我希望能够使用线程设置“testV”值。 看起来 Thread 确实设置了值,但是当从 main 方法中检索 testV 值时,它什么也没给出。 这是为什么呢?
【问题讨论】:
-
从主线程读取可能发生在其他线程有机会写入值之前