//标记为 ThreadStaticAttribute 的静态字段不在线程之间共享。
// 可以测试以下的实例有助于理解
class Program
{
[ThreadStatic]
static int i;
static Random r = new Random(); public static void ThreadProc()
{
i = r.Next(1, 10); while (true)
{
Console.WriteLine("{0}:{1}", Thread.CurrentThread.Name, i);
Thread.Sleep(500);
}
} public static void Main()
{
for (int i = 0; i < 2; i++)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Name = "T" + i.ToString();
t.Start();
} Console.WriteLine("Press Enter key to exit...");
Console.ReadLine();
}
}
// 可以测试以下的实例有助于理解
class Program
{
[ThreadStatic]
static int i;
static Random r = new Random(); public static void ThreadProc()
{
i = r.Next(1, 10); while (true)
{
Console.WriteLine("{0}:{1}", Thread.CurrentThread.Name, i);
Thread.Sleep(500);
}
} public static void Main()
{
for (int i = 0; i < 2; i++)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Name = "T" + i.ToString();
t.Start();
} Console.WriteLine("Press Enter key to exit...");
Console.ReadLine();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货