存储io单线程和多线程_在线程上存储东西

存储io单线程和多线程

存储io单线程和多线程

There's so many places to put stuff. I think one of the most difficult things for new .NET developers is deciding where to store objects and bits of data.

有很多地方可以放东西。 我认为对于新.NET开发人员而言,最困难的事情之一就是确定将对象和数据位存储在何处。

There's certainly the obvious starting points:

当然有明显的起点:

  • Local variables: Variables exist for a short period of time, have "scope" and may live only for the life of a method call. No threading issues.

    局部变量:变量存在的时间很短,具有“作用域”,并且可能仅在方法调用的生命期内有效。 没有线程问题。

  • Instance class fields: Variables exist for the life of the class. Threading issues can come up if multiple threads go poking around.

    实例类字段:在类的生存期内存在变量。 如果出现多个线程,线程问题可能会出现。

  • Static local variables: Variables are stored in a single location and are shared between instances. Threading issues galore as you are responsible for serializing access to folks messing with these variables.

    静态局部变量:变量存储在单个位置,并且在实例之间共享。 线程问题异常丰富,因为您负责序列化处理这些变量的人员的访问权限。

  • Variables marked with [ThreadStatic]: These should NEVER be used from within ASP.NET. I've blogged about its evil before. Details From MSDN - A static (Shared in Visual Basic) field marked with ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value.

    标有[ThreadStatic]的变量:切勿在ASP.NET中使用这些变量。 我以前曾写过关于它的邪恶的博客。 MSDN中的详细信息-线程之间不共享标记为ThreadStaticAttribute的静态(在Visual Basic中为Shared)字段。 每个执行线程都有一个单独的字段实例,并独立设置和获取该字段的值。 如果在不同的线程上访问该字段,它将包含一个不同的值。

  • CallContext: These are a set of properties that are carried along within the execution code path. You can add variables to the CallContext as it flows through execution. You can add objects into the CallContext as if it were a Hashtable, using .SetData() and .GetData(). As it moves over/between application domains, it is cloned. Objects that implement ILogicalThreadAffinative will go along for the ride.

    CallContext:这些是在执行代码路径中附带的一组属性。 您可以在执行过程中向CallContext添加变量。 您可以使用.SetData()和.GetData()将对象添加到CallContext中,就好像它是哈希表一样。 当它在应用程序域之间移动时,将对其进行克隆。 实现ILogicalThreadAffinative的对象将继续前进。

  • ThreadLocalStorage: This is something I don't see used a lot, as it's pretty specialized. Dino Chiesa had this cool example. He wanted to provide a factory that could be used by multiple threads to retrieve an object instance, but he wanted each thread to get their own reusable copy. He uses a LocalDataStoreSlot to store/cache objects on a thread-by-thread basis. This way he can always call GetProxy from his code, and ensure that only one copy will exist per thread.

    ThreadLocalStorage:这是我不常用的东西,因为它非常专业。 迪诺·基耶萨(Dino Chiesa)有一个很酷的例子。 他想提供一个可由多个线程用来检索对象实例的工厂,但是他希望每个线程都能获得自己的可重用副本。 他使用LocalDataStoreSlot逐个线程地存储/缓存对象。 这样,他可以始终从代码中调用GetProxy,并确保每个线程仅存在一个副本。

    • public static System.LocalDataStoreSlot Slot= System.Threading.Thread.AllocateNamedDataSlot("webutil.proxy");

      公共静态System.LocalDataStoreSlot Slot = System.Threading.Thread.AllocateNamedDataSlot(“ webutil.proxy”);

      public static System.Net.WebProxy GetProxy()

      公共静态System.Net.WebProxy GetProxy()

      {

      {

          object PutativeProxy = System.Threading.Thread.GetData(Slot);

      对象PutativeProxy = System.Threading.Thread.GetData(Slot);

          if (PutativeProxy==null)

      如果(PutativeProxy == null )

          {

      {

              string ProxyString =     System.Configuration.ConfigurationSettings.AppSettings[GetConfigKey("proxy")];

      字符串ProxyString = System.Configuration.ConfigurationSettings.AppSettings [GetConfigKey(“ proxy”)]];

              if ((ProxyString == null) || (ProxyString == "")) return null;

      if (((ProxyString == null )||(ProxyString ==“”)))返回null ;

              PutativeProxy = new System.Net.WebProxy(ProxyString, true);

      PutativeProxy = new System.Net.WebProxy(ProxyString, true );

              System.Threading.Thread.SetData(Slot, PutativeProxy); 

      System.Threading.Thread.SetData(Slot,PutativeProxy);

          }

      }

          return (System.Net.WebProxy) PutativeProxy;

      返回(System.Net.WebProxy)PutativeProxy;

      }

      }

翻译自: https://www.hanselman.com/blog/storing-things-on-threads

存储io单线程和多线程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值