C# .NET Framework 3.5 下 Task 与 EventWaitHandle 的简单例子

只是把 Semaphore 换成了 EventWaitHandle。请与这个贴子中的例子进行比较:https://www.cnblogs.com/pencilstart/p/15865478.html

注意,由于 .NET 3.5下并没有官方实现的 Task 库,所以,是通过 VS 中 NuGet 取得的 非官方 实现的 Task 库,调用接口与官方.NET 4.0 后的应该是差不多的。

C# .NET Framework 3.5 下 Task 与 EventWaitHandle 的简单例子

EventWaitHandle 初始化时,初始值为 signaled 的状态。

 1 using System;
 2 using System.Threading;
 3 using System.Threading.Tasks;
 4 
 5 namespace testEventWaitHandle
 6 {
 7     class Program
 8     {
 9         EventWaitHandle ewh = new EventWaitHandle(true, EventResetMode.AutoReset);
10 
11         static int count = 0;
12 
13         void test()
14         {
15             object param1 = new object[] { "Thread A", 0 };
16             object param2 = new object[] { "Thread B", 500 };
17 
18             Action<object> a1 = new Action<object>(TaskProc);
19             Action<object> a2 = new Action<object>(TaskProc);
20 
21             Task task1 = new TaskFactory().StartNew(a1, param1);
22             Task task2 = new TaskFactory().StartNew(a1, param2);
23 
24             Task.WaitAll(task1, task2);
25         }
26 
27         static void Main(string[] args)
28         {
29             Program p = new Program();
30 
31             p.test();
32 
33             Console.WriteLine("main end, mainthreadid = {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
34         }
35 
36         void TaskProc(object args)
37         {
38             string name = (string)((object[])args)[0];
39             int startdelay = (int)((object[])args)[1];
40 
41             Console.WriteLine("{0}: COME IN, threadid = {1}", name, System.Threading.Thread.CurrentThread.ManagedThreadId);
42 
43             System.Threading.Thread.Sleep(startdelay);
44 
45             Console.WriteLine("{0}: START, threadid = {1}", name, System.Threading.Thread.CurrentThread.ManagedThreadId);
46 
47             for (int i = 0; i < 10; ++i)
48             {
49                 ewh.WaitOne();
50 
51                 Random r = new Random();
52 
53                 int s = r.Next(50, 1000);
54 
55                 count = count + 1;
56                 Console.WriteLine("{0}: i = {1}, sleep({2}), threadid = {3}, count = {4}", name, i, s, System.Threading.Thread.CurrentThread.ManagedThreadId, count);
57 
58                 System.Threading.Thread.Sleep(s);
59 
60                 ewh.Set();
61             }
62 
63             Console.WriteLine("{0}: END, threadid = {1}", name, System.Threading.Thread.CurrentThread.ManagedThreadId);
64         }
65     }
66 }
67 
68 //Thread A: COME IN, threadid = 3
69 //Thread A: START, threadid = 3
70 //Thread A: i = 0, sleep(566), threadid = 3, count = 1
71 //Thread B: COME IN, threadid = 4
72 //Thread B: START, threadid = 4
73 //Thread B: i = 0, sleep(530), threadid = 4, count = 2
74 //Thread A: i = 1, sleep(918), threadid = 3, count = 3
75 //Thread B: i = 1, sleep(610), threadid = 4, count = 4
76 //Thread A: i = 2, sleep(759), threadid = 3, count = 5
77 //Thread B: i = 2, sleep(928), threadid = 4, count = 6
78 //Thread A: i = 3, sleep(961), threadid = 3, count = 7
79 //Thread B: i = 3, sleep(684), threadid = 4, count = 8
80 //Thread A: i = 4, sleep(935), threadid = 3, count = 9
81 //Thread B: i = 4, sleep(359), threadid = 4, count = 10
82 //Thread A: i = 5, sleep(883), threadid = 3, count = 11
83 //Thread B: i = 5, sleep(389), threadid = 4, count = 12
84 //Thread A: i = 6, sleep(148), threadid = 3, count = 13
85 //Thread B: i = 6, sleep(621), threadid = 4, count = 14
86 //Thread A: i = 7, sleep(161), threadid = 3, count = 15
87 //Thread B: i = 7, sleep(976), threadid = 4, count = 16
88 //Thread A: i = 8, sleep(430), threadid = 3, count = 17
89 //Thread B: i = 8, sleep(261), threadid = 4, count = 18
90 //Thread A: i = 9, sleep(682), threadid = 3, count = 19
91 //Thread A: END, threadid = 3
92 //Thread B: i = 9, sleep(139), threadid = 4, count = 20
93 //Thread B: END, threadid = 4
94 //main end, mainthreadid = 1
95 //请按任意键继续. . .

 

上一篇:58 Scala高阶函数


下一篇:56 Scala面向对象