好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在WEB程序中小心使用 “ThreadStatic”

在WEB程序中小心使用 “ThreadStatic”

在WEB程序中小心使用 “ThreadStatic”

场景

在WEB系统开发中,我们经常面对这样的需求:如何在一个请求中共享数据或对象实例?之前我都会用HttpContext.Current.Items。然而有一天我发现了两个事实:一、每个请求都是在一个线程中执行的;二、[ThreadStatic]可以标注某个静态字段为每个线程提供独立的存储。面对这两个发现,我得出了这个结论:可以用[ThreadStatic]替换HttpContext.Current.Items。

问题

可以用[ThreadStatic]替换HttpContext.Current.Items吗?

实验

实验素材

  1   using   System;
   2   using   System.Collections.Generic;
   3   using   System.Linq;
   4   using   System.Web;
   5   using   System.Web.UI;
   6   using   System.Web.UI.WebControls;
   7  
  8   using   System.Threading;
   9   using   System.IO;
  10  
 11   namespace   WebThreadStaticStudy
  12   {
  13       public   partial   class   Test : System.Web.UI.Page
  14       {
  15           [ThreadStatic]
  16           private   static  DateTime?  Now;
  17           private   static   int  _Times =  1  ;
  18  
 19           protected   void  Page_Load( object   sender, EventArgs e)
  20           {
  21               if  (Now ==  null  )
  22               {
  23                  Now =  DateTime.Now;
  24               }
  25  
 26               if  (HttpContext.Current.Items[ "  Now  " ] ==  null  )
  27               {
  28                  HttpContext.Current.Items[ "  Now  " ] =  DateTime.Now;
  29               }
  30  
 31               string  content= string .Format( "  第{0}次,线程:{1},ThreadStatic时间:{2},HttpContext.Current.Items时间:{3}。\r\n  " 
 32                  , _Times++
 33                   , Thread.CurrentThread.ManagedThreadId
  34                   , Now
  35                  , HttpContext.Current.Items[ "  Now  "  ]);
  36  
 37               this  .Response.Write(content);
  38  
 39              File.AppendAllText( @"  F:\学习项目\规律化学习\WebThreadStaticStudy\WebThreadStaticStudy\Log.txt  "  , content);
  40           }
  41       }
  42  }

实验结果

  1  第1次,线程:8,ThreadStatic时间:2013/5/3 11:22:06,HttpContext.Current.Items时间:2013/5/3 11:22:06 。
   2  第2次,线程:10,ThreadStatic时间:2013/5/3 11:22:08,HttpContext.Current.Items时间:2013/5/3 11:22:08 。
   3  第3次,线程:8,ThreadStatic时间:2013/5/3 11:22:06,HttpContext.Current.Items时间:2013/5/3 11:22:08 。
   4  第4次,线程:10,ThreadStatic时间:2013/5/3 11:22:08,HttpContext.Current.Items时间:2013/5/3 11:22:09 。
   5  第5次,线程:11,ThreadStatic时间:2013/5/3 11:22:10,HttpContext.Current.Items时间:2013/5/3 11:22:10 。
   6  第6次,线程:10,ThreadStatic时间:2013/5/3 11:22:08,HttpContext.Current.Items时间:2013/5/3 11:22:10 。
   7  第7次,线程:8,ThreadStatic时间:2013/5/3 11:22:06,HttpContext.Current.Items时间:2013/5/3 11:22:10 。
   8  第8次,线程:11,ThreadStatic时间:2013/5/3 11:22:10,HttpContext.Current.Items时间:2013/5/3 11:22:11 。
   9  第9次,线程:8,ThreadStatic时间:2013/5/3 11:22:06,HttpContext.Current.Items时间:2013/5/3 11:22:12 。
  10  第10次,线程:11,ThreadStatic时间:2013/5/3 11:22:10,HttpContext.Current.Items时间:2013/5/3 11:22:12。

结论

不可以用[ThreadStatic]替换HttpContext.Current.Items。

原因分析

WEB服务器用线程池执行每个请求,多个不同时段执行的请求还是会共享同一个线程。

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于在WEB程序中小心使用 “ThreadStatic”的详细内容...

  阅读:42次