C# ThreadStatic vs ThreadLocal<T>:泛型比属性好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18333885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ThreadStatic v.s. ThreadLocal<T>: is generic better than attribute?
提问by user2341923
[ThreadStatic]
is defined using attribute while ThreadLocal<T>
uses generic.
Why different design solutions were chosen?
What are the advantages and disadvantages of using generic over attributes in this case?
[ThreadStatic]
使用属性定义,而ThreadLocal<T>
使用泛型。为什么选择不同的设计解决方案?在这种情况下使用泛型而不是属性有什么优点和缺点?
采纳答案by Jim Mischel
Something the blog post noted in the comments doesn't make explicit, but I find to be very important, is that [ThreadStatic]
doesn't automatically initialize things for every thread. For example, say you have this:
评论中指出的博客文章没有明确说明,但我发现非常重要的是,[ThreadStatic]
它不会自动为每个线程初始化事物。例如,假设你有这个:
[ThreadStatic]
private static int Foo = 42;
The first thread that uses this will see Foo
initialized to 42
. But subsequent threads will not. The initializer works for the first thread only. So you end up having to write code to check if it's initialized.
使用它的第一个线程将看到Foo
初始化为42
. 但后续线程不会。初始值设定项仅适用于第一个线程。因此,您最终不得不编写代码来检查它是否已初始化。
ThreadLocal<T>
solves that problem by letting you supply an initialization function (as Reed's blog shows) that's run before the first time the item is accessed.
ThreadLocal<T>
通过让您提供在第一次访问项目之前运行的初始化函数(如 Reed 的博客所示)来解决该问题。
In my opinion, there is no advantage to using [ThreadStatic]
instead of ThreadLocal<T>
.
在我看来,使用[ThreadStatic]
代替ThreadLocal<T>
.
回答by marai
ThreadStatic Initialize only on first thread, ThreadLocal Initialize for each thread. Below is the simple demonstration:
ThreadStatic 仅在第一个线程上初始化,ThreadLocal 为每个线程初始化。下面是简单的演示:
public static ThreadLocal<int> _threadlocal =
new ThreadLocal<int>(() =>
{
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();
}
回答by Sanjeev
The main idea behind ThreadStatic is to maintain a separate copyof the variable for each thread.
ThreadStatic 背后的主要思想是为每个线程维护一个单独的变量副本。
class Program
{
[ThreadStatic]
static int value = 10;
static void Main(string[] args)
{
value = 25;
Task t1 = Task.Run(() =>
{
value++;
Console.WriteLine("T1: " + value);
});
Task t2 = Task.Run(() =>
{
value++;
Console.WriteLine("T2: " + value);
});
Task t3 = Task.Run(() =>
{
value++;
Console.WriteLine("T3: " + value);
});
Console.WriteLine("Main Thread : " + value);
Task.WaitAll(t1, t2, t3);
Console.ReadKey();
}
}
In the above snippet, we have a separate copy of value
for each thread, including the main thread.
在上面的代码片段中,我们value
为每个线程都有一个单独的副本,包括主线程。
So, a ThreadStatic variable will be initialized to its default value on other threads except the thread on which it is created.
因此,一个 ThreadStatic 变量将在除创建它的线程之外的其他线程上初始化为其默认值。
If we want to initialize the variable on each thread in our own way, use ThreadLocal.
如果我们想以自己的方式初始化每个线程上的变量,请使用 ThreadLocal。