在.net中,在两个线程之间共享静态变量的最佳方法是什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace StackOverflow
{
class ThreadStaticProgram
{
static string threadStaticVariable = "";
static void Main(string[] args)
{
Console.WriteLine(" Main Thread Before {0} ", threadStaticVariable);
threadStaticVariable = " Main Thread ";
Console.WriteLine(" Main Thread Before For Loop = {0} ", threadStaticVariable);
Thread[] threads = new Thread[3];
for (int i = 0; i < 3; i++)
{
threads[i] = new Thread(delegate(object j)
{
Console.WriteLine(" Thread{0} before = {1} ", j, threadStaticVariable);
threadStaticVariable = " Thread " + j;
Console.WriteLine(" Thread{0} after ={1} ", j, threadStaticVariable);
}
);
threads[i].Start(i);
}
Array.ForEach(threads, delegate(Thread t) { t.Join(); });
Console.WriteLine(" Main Thread after For Loop = {0} ", threadStaticVariable);
Console.ReadLine();
}
}
}
问题二-什么是线程本地存储?