Thread.SetData与ThreadStatic

今天在这里看到一篇关于Thread.SetData的文章,忽然想起了ThreadStatic特性一样可使变量线程唯一,于是修改了一下代码测试下:ExpandedBlockStart.gif
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading;
 6 
 7 namespace ThreadSetDataTest
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Thread[] t = new Thread[4];
14             for (int i = 0; i < t.Length; i++)
15             {
16                 t[i] = new Thread( new ThreadStart(Slot.SlotTest));
17                 t[i].Start();
18             }
19         }
20     }
21 
22     class Slot
23     {
24         static Random randomGenerator = new Random();
25 
26         [ThreadStatic]
27         public static int threadStaticData;
28 
29         public static void SlotTest()
30         {
31             Thread.SetData(Thread.GetNamedDataSlot("Random"), randomGenerator.Next(0200));
32             threadStaticData = randomGenerator.Next(10100);
33 
34             Console.WriteLine("Data is Thread_{0}'s data solt :{1,3}, ThreadStatic Data is {2}",
35                 AppDomain.GetCurrentThreadId(),
36                 Thread.GetData(Thread.GetNamedDataSlot("Random")).ToString(), threadStaticData);
37 
38             Thread.Sleep(1000);
39 
40             Console.WriteLine("Data is Thread_{0}'s data solt :{1,3},ThreadStatic Data is {2}",
41                 AppDomain.GetCurrentThreadId(),
42                 Thread.GetData(Thread.GetNamedDataSlot("Random")).ToString(), threadStaticData);
43 
44             Thread.Sleep(1000);
45 
46             Other o = new Other();
47             o.ShowSoltData();
48         }
49 
50     }
51 
52     class Other
53     {
54         public void ShowSoltData()
55         {
56             Console.WriteLine("Data is Thread_{0}'s data solt :{1,3},ThreadStatic Data is {2}",
57                AppDomain.GetCurrentThreadId(),
58                Thread.GetData(Thread.GetNamedDataSlot("Random")).ToString(), Slot.threadStaticData);
59         }
60     }
61 

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Linq;
 4  using  System.Text;
 5  using  System.Threading;
 6 
 7  namespace  ThreadSetDataTest
 8  {
 9       class  Program
10      {
11           static   void  Main( string [] args)
12          {
13              Thread[] t  =   new  Thread[ 4 ];
14               for  ( int  i  =   0 ; i  <  t.Length; i ++ )
15              {
16                  t[i]  =   new  Thread(  new  ThreadStart(Slot.SlotTest));
17                  t[i].Start();
18              }
19          }
20      }
21 
22       class  Slot
23      {
24           static  Random randomGenerator  =   new  Random();
25 
26           ///   <summary>
27           ///  线程唯一的变量
28           ///   </summary>
29          [ThreadStatic]
30           public   static   int  threadStaticData;
31 
32           public   static   void  SlotTest()
33          {
34               //  在每条线程中设置随机的值
35              Thread.SetData(Thread.GetNamedDataSlot( " Random " ), randomGenerator.Next( 0 200 ));
36 
37               //  同样在每条线程中设置随机的值
38              threadStaticData  =  randomGenerator.Next( 10 100 );
39 
40              Console.WriteLine( " Data is Thread_{0}'s data solt :{1,3}, ThreadStatic Data is {2} " ,
41                  AppDomain.GetCurrentThreadId(),
42                  Thread.GetData(Thread.GetNamedDataSlot( " Random " )).ToString(), threadStaticData);
43 
44              Thread.Sleep( 1000 );
45 
46              Console.WriteLine( " Data is Thread_{0}'s data solt :{1,3},ThreadStatic Data is {2} " ,
47                  AppDomain.GetCurrentThreadId(),
48                  Thread.GetData(Thread.GetNamedDataSlot( " Random " )).ToString(), threadStaticData);
49 
50              Thread.Sleep( 1000 );
51 
52              Other o  =   new  Other();
53              o.ShowSoltData();
54          }
55 
56      }
57 
58       class  Other
59      {
60           public   void  ShowSoltData()
61          {
62              Console.WriteLine( " Data is Thread_{0}'s data solt :{1,3},ThreadStatic Data is {2} " ,
63                 AppDomain.GetCurrentThreadId(),
64                 Thread.GetData(Thread.GetNamedDataSlot( " Random " )).ToString(), Slot.threadStaticData);
65          }
66      }
67  }
68 
出来结果:

posted on 2008-09-24 21:28 非鸟^ 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/phineux/archive/2008/09/24/1298340.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值