# 4.0中的并行计算和多线程详解(二)

## 多线程部分

### 一、简单使用

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.         }

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

t带有t1,t2,t3三个子线程，执行t的时候t1,t2,t3可并行处理，t必须等待t1,t2,t3都执行完毕后才能结束。

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.         }

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.         }

### 五、其他

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.                          });