0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 多线程部分

### 一、简单使用

开启一个线程，执行循环方法，返回结果。开始线程为Start()，等待线程结束为Wait()。

Code
1.         /// <summary>
3.         /// </summary>
4.         private void Demo1()
5.         {
6.             int i = 0;
7.             Random r = new Random(DateTime.Now.Second);
9.             {
10.                 for (int v = 0; v < 100; v++)
11.                     i += r.Next(100);
12.             });
13.             t.Start();
14.             t.Wait();
17.         }

Code
1.         /// <summary>
3.         /// </summary>
4.         private void Demo2()
5.         {
6.             Random r = new Random(DateTime.Now.Second);
8.             {
9.                 int i = 0;
10.                 for (int v = 0; v < 100; v++)
11.                     i += r.Next(100);
12.                 return i;
13.             });
14.             t.Start();
17.         }

### 二、线程执行完毕后调用另一个线程

Code
1.         /// <summary>
3.         /// </summary>
4.         private void Demo3()
5.         {
6.             Random r = new Random(DateTime.Now.Second);
8.             {
9.                 int i = 0;
10.                 for (int v = 0; v < 100; v++)
11.                     i += r.Next(100);
12.                 return i;
13.             });
15.             {
17.             });
18.             t.Start();
20.         }

也可以直接链式的写下去，代码如下：

Code
1.         /// <summary>
3.         /// </summary>
4.         private void Demo4()
5.         {
6.             Random r = new Random(DateTime.Now.Second);
8.             {
9.                 int i = 0;
10.                 for (int v = 0; v < 100; v++)
11.                     i += r.Next(100);
12.                 return i;
13.             });
15.             {
17.             });
19.             {
21.             });
22.             t.Start();
24.         }

### 三、带有父子关系的线程/多线程并行开启

Code
1.         /// <summary>
3.         ///
4.         /// 值                说明
9.         /// </summary>
10.         private void Demo5()
11.         {
13.             {
15.                 {
16.                     int i = 0;
17.                     Random r = new Random(DateTime.Now.Second);
18.                     for (int v = 0; v < 100; v++)
19.                         i += r.Next(100);
20.                     return i;
23.                 {
24.                     int i = 0;
25.                     Random r = new Random(DateTime.Now.Second);
26.                     for (int v = 0; v < 100; v++)
27.                         i += r.Next(100);
28.                     return i;
31.                 {
32.                     int i = 0;
33.                     Random r = new Random(DateTime.Now.Second);
34.                     for (int v = 0; v < 100; v++)
35.                         i += r.Next(100);
36.                     return i;
38.                 t1.Start();
39.                 t2.Start();
40.                 t3.Start();
41.                 return t1.Result + t2.Result + t3.Result;
42.             });
43.             t.Start();
44.             t.Wait();
47.         }

结论3：多个线程的同时开启在这里也很方便，也不用担心同步等问题。

这个很复杂，就不多说了，代码中有比较详细的介绍。

Code
1.         /// <summary>
3.         ///
5.         /// 值                              说明
14.         ///
16.         /// 属性            说明
20.         /// </summary>
21.         private void Demo6()
22.         {
23.             CancellationTokenSource cts = new CancellationTokenSource();
24.             var ctoken = cts.Token;
26.             {
27.                 for (int v = 0; v < 10; v++)
28.                 {
29.                     if (ctoken.IsCancellationRequested)
30.                     {
31.                         //第一种写法
32.                         //这个会抛出异常
33.                         ctoken.ThrowIfCancellationRequested();
34.                         //另一种写法
35.                         //这个不会返回异常，但是获取不到是否是中断还是执行完毕。
36.                         //return;
37.                     }
39.                     Console.WriteLine(v);
40.                 }
41.             }, ctoken);
42.             t1.Start();
44.             cts.Cancel();
45.             try
46.             {
47.                 t1.Wait();
48.             }
49.             catch
50.             {
51.                 if (t1.IsCanceled)
52.                     Console.WriteLine("cancel");
53.             }
55.             cts.Dispose();
56.         }

结论4：中断很复杂，但是对一般逻辑来说，是没有很大必要的。

### 五、其他

Code
1.         /// <summary>
3.         /// ContinueWhenAny和ContinueWhenAll
6.         /// </summary>
7.         private void Demo7()
8.         {
10.             {
11.                 int total = 0;
12.                 for (int i = 0; i < 10; i++)
13.                     total += i;
16.             });
18.             {
19.                 int total = 0;
20.                 for (int i = 10; i < 20; i++)
21.                     total += i;
24.             });
27.                          {
29.                              {
31.                              }
32.                          });