1. <small id='hJsWi'></small><noframes id='hJsWi'>

      <i id='hJsWi'><tr id='hJsWi'><dt id='hJsWi'><q id='hJsWi'><span id='hJsWi'><b id='hJsWi'><form id='hJsWi'><ins id='hJsWi'></ins><ul id='hJsWi'></ul><sub id='hJsWi'></sub></form><legend id='hJsWi'></legend><bdo id='hJsWi'><pre id='hJsWi'><center id='hJsWi'></center></pre></bdo></b><th id='hJsWi'></th></span></q></dt></tr></i><div id='hJsWi'><tfoot id='hJsWi'></tfoot><dl id='hJsWi'><fieldset id='hJsWi'></fieldset></dl></div>

        <legend id='hJsWi'><style id='hJsWi'><dir id='hJsWi'><q id='hJsWi'></q></dir></style></legend>
      1. <tfoot id='hJsWi'></tfoot>
          <bdo id='hJsWi'></bdo><ul id='hJsWi'></ul>

        ThreadStatic 与 ThreadStatic 对比ThreadLocal&lt;T&gt;:泛

        ThreadStatic v.s. ThreadLocallt;Tgt;: is generic better than attribute?(ThreadStatic 与 ThreadStatic 对比ThreadLocallt;Tgt;:泛型比属性好吗?)
      2. <small id='ulqAU'></small><noframes id='ulqAU'>

          <bdo id='ulqAU'></bdo><ul id='ulqAU'></ul>
          <i id='ulqAU'><tr id='ulqAU'><dt id='ulqAU'><q id='ulqAU'><span id='ulqAU'><b id='ulqAU'><form id='ulqAU'><ins id='ulqAU'></ins><ul id='ulqAU'></ul><sub id='ulqAU'></sub></form><legend id='ulqAU'></legend><bdo id='ulqAU'><pre id='ulqAU'><center id='ulqAU'></center></pre></bdo></b><th id='ulqAU'></th></span></q></dt></tr></i><div id='ulqAU'><tfoot id='ulqAU'></tfoot><dl id='ulqAU'><fieldset id='ulqAU'></fieldset></dl></div>
          <tfoot id='ulqAU'></tfoot>

            • <legend id='ulqAU'><style id='ulqAU'><dir id='ulqAU'><q id='ulqAU'></q></dir></style></legend>

                <tbody id='ulqAU'></tbody>

                  本文介绍了ThreadStatic 与 ThreadStatic 对比ThreadLocal&lt;T&gt;:泛型比属性好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  [ThreadStatic] 使用属性定义,而 ThreadLocal<T> 使用泛型.为什么选择不同的设计解决方案?在这种情况下使用泛型优于属性有什么优缺点?

                  [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] 不会自动为每个线程初始化事物.例如,假设你有这个:

                  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]
                  private static int Foo = 42;
                  

                  使用它的第一个线程将看到 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.

                  ThreadLocal<T> 通过让您提供一个在第一次访问项目之前运行的初始化函数(如 Reed 的博客所示)来解决该问题.

                  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.

                  在我看来,使用 [ThreadStatic] 代替 ThreadLocal<T> 没有任何优势.

                  In my opinion, there is no advantage to using [ThreadStatic] instead of ThreadLocal<T>.

                  这篇关于ThreadStatic 与 ThreadStatic 对比ThreadLocal&lt;T&gt;:泛型比属性好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                    <bdo id='2ev3p'></bdo><ul id='2ev3p'></ul>

                      <small id='2ev3p'></small><noframes id='2ev3p'>

                        <tbody id='2ev3p'></tbody>
                      • <legend id='2ev3p'><style id='2ev3p'><dir id='2ev3p'><q id='2ev3p'></q></dir></style></legend>
                      • <i id='2ev3p'><tr id='2ev3p'><dt id='2ev3p'><q id='2ev3p'><span id='2ev3p'><b id='2ev3p'><form id='2ev3p'><ins id='2ev3p'></ins><ul id='2ev3p'></ul><sub id='2ev3p'></sub></form><legend id='2ev3p'></legend><bdo id='2ev3p'><pre id='2ev3p'><center id='2ev3p'></center></pre></bdo></b><th id='2ev3p'></th></span></q></dt></tr></i><div id='2ev3p'><tfoot id='2ev3p'></tfoot><dl id='2ev3p'><fieldset id='2ev3p'></fieldset></dl></div>
                          <tfoot id='2ev3p'></tfoot>