[ThreadStatic] is defined using attribute while ThreadLocal uses generic.
Why different design solutions were chosen?
What are the advan
ThreadStatic Initialize only on first thread, ThreadLocal Initialize for each thread. Below is the simple demonstration:
public static ThreadLocal _threadlocal =
new ThreadLocal(() =>
{
return Thread.CurrentThread.ManagedThreadId;
});
public static void Main()
{
new Thread(() =>
{
for (int x = 0; x < _threadlocal.Value; x++)
{
Console.WriteLine("First Thread: {0}", x);
}
}).Start();
new Thread(() =>
{
for (int x = 0; x < _threadlocal.Value; x++)
{
Console.WriteLine("Second Thread: {0}", x);
}
}).Start();
Console.ReadKey();
}
