Let's go

线程

今天师傅教了一个线程入门知识点,以备后面使用:

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5                 Stopwatch watch = Stopwatch.StartNew();
 6 
 7                 ////业务1  拿到起返回值
 8                 //var t1 = T1();
 9                 //Console.WriteLine("hhh");
10                 ////业务2   拿到返回值
11                 //var t2 = T2();
12                 //Console.WriteLine("hhh2");
13                 //string t = t1 + t2;
14                 
15                 var task1 = Task.Run(() =>
16                     {
17                         return T1();
18                     });
19                 Console.WriteLine("hhh");
20 
21                 var task2 = Task.Run(() =>
22                 {
23                     return T2();
24                 });
25                 Console.WriteLine("hhh2");
26 
27                 Task.WhenAll(task1, task2);
28 
29                 string t = task1.Result + task2.Result;
30             
31 
32             //要把业务1的返回值和业务2的返回值拼接起来,输出
33 
34 
35             watch.Stop();
36             var time = watch.ElapsedMilliseconds;
37 
38             Console.WriteLine(t);
39             Console.WriteLine("耗时:{0}", watch.ElapsedMilliseconds);
40 
41             Console.ReadKey();
42 
43         }
44 
45         public static string T1()
46         {
47             Thread.Sleep(3000);
48 
49             return "Msg1";
50         }
51 
52         public static string T2()
53         {
54             Thread.Sleep(5000);
55 
56             return "Msg2";
57         }
58     }
59        
Task.Run()

输入结果:

 

task1和task2都为子线程。而直接输出hhh和hhh2,分别占用的是task1和task2子线程的时间。主线程走到task1和task2只是执行了开启(秒开)。

执行到 Task.WhenAll(task1, task2);的时候,才输出共消耗的时间(等待所有任务结束使用)

格式化显示时间:

1            {   //格式化显示时间
2                 TimeSpan ts = watch.Elapsed;
3                 // Format and display the TimeSpan value.
4                 string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
5                     ts.Hours, ts.Minutes, ts.Seconds,
6                     ts.Milliseconds / 10);
7                 Console.WriteLine("RunTime " + elapsedTime);
8             }        

 

task1 和  task2 两个任务在定义之后就已经马不停蹄的在子线程中运行了,Task.WaitAll 是一个等待的过程,参数就是Task参数,一旦全部执行完毕了,就继续往下执行,这里是阻塞的,还是比较好理解的。

这样的话WaitAny就很好理解了,就是参数里面的Task 有任意一个执行完成,就继续执行后面的逻辑

 

WhenAll

WhenAll其实跟WaitAll是为了实现一样的功能,只是在WaitAll基础上又做了一层包装,

                Task.WhenAll(task1, task2).ContinueWith(p =>
                {
                   
                }, TaskContinuationOptions.OnlyOnRanToCompletion);

 

 

1.WaitAny(执行的线程等待其中任何一个线程执行完毕即可执行)

2.WaitAll(执行的线程等待其中所有线程执行完毕方可执行)

 1  var t1=Task.Run(() =>
 2             {
 3                 Console.WriteLine("demo1,当前线程id为:" + Thread.CurrentThread.ManagedThreadId + ",当前时间为" + watch.ElapsedMilliseconds);
 4             });
 5             var t2 = Task.Run(() =>
 6             {
 7                 Console.WriteLine("demo2,当前线程id为:" + Thread.CurrentThread.ManagedThreadId + ",当前时间为" + watch.ElapsedMilliseconds);
 8             });
 9             var t3 = Task.Run(() =>
10             {
11                 Console.WriteLine("demo3,当前线程id为:" + Thread.CurrentThread.ManagedThreadId + ",当前时间为" + watch.ElapsedMilliseconds);
12             });
13             var t4 = Task.Run(() =>
14             {
15                 Console.WriteLine("demo4,当前线程id为:" + Thread.CurrentThread.ManagedThreadId + ",当前时间为" + watch.ElapsedMilliseconds);
16             });
17             var t5 = Task.Run(() =>
18             {
19                 Console.WriteLine("demo5,当前线程id为:" + Thread.CurrentThread.ManagedThreadId + ",当前时间为" + watch.ElapsedMilliseconds);
20             });
21             var t6= Task.Run(() =>
22             {
23                 Console.WriteLine("demo6,当前线程id为:" + Thread.CurrentThread.ManagedThreadId + ",当前时间为" + watch.ElapsedMilliseconds);
24             });
25             Task.WaitAll(t1,t2,t3,t4,t5,t6);
26             Console.WriteLine("执行完毕,WaitAll,线程id为:" + Thread.CurrentThread.ManagedThreadId + "耗时为:" + watch.ElapsedMilliseconds);
27             Console.ReadKey();
View Code

 

 

posted @ 2018-07-03 15:17  chenze  阅读(241)  评论(1编辑  收藏  举报
有事您Q我