最近项目需求多线程,使用了线程池和Thread开启新线程,总不符合我的想要的,于是看了下新特性Task
废话不多说,直接贴代码,实验的demo 只作参考
1 internal class Program 2 { 3 private static void Main(string[] args) 4 { 5 Console.WriteLine("Hello World!"); 6 Console.WriteLine($"开始启动线程开始时间{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); 7 string[] str = TestTask(); 8 foreach (string s in str) 9 { 10 Console.WriteLine($"获取线程返回结果{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); 11 Console.WriteLine(s); 12 13 } 14 15 Console.WriteLine($"主线程结束:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); 16 Console.ReadKey(); 17 } 18 19 public static string[] TestTask() 20 { 21 ObjectThreadList objectThread = new ObjectThreadList(); 22 Task<string[]> t = new Task<string[]>(() => 23 { 24 string[] str = new string[3]; 25 new Task(async () => str[0] = await objectThread.ChildThreadOne("dss", "dsds", "dsd"), TaskCreationOptions.AttachedToParent).Start(); 26 new Task(async () => str[1] = await objectThread.ChildThreadTwo("fdssf", "dfsewr", "rewdf"), TaskCreationOptions.AttachedToParent).Start(); 27 new Task(async () => str[2] = await objectThread.ChildThreadThree(), TaskCreationOptions.AttachedToParent).Start(); 28 return str; 29 }); 30 t.Start(); 31 Console.WriteLine("******************未获取结果之前*******************"); 32 foreach (string s in t.Result) 33 { 34 Console.WriteLine("========================函数内部==============================="); 35 Console.WriteLine(s); 36 Console.WriteLine("========================函数内部==============================="); 37 } 38 return t.Result; 39 } 40 } 41 public class ObjectThreadList 42 { 43 public async Task<string> ChildThreadOne(string a, string b, string c) 44 { 45 string str = a + b + c; 46 Console.WriteLine($"子任务一 未暂停之前{DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 47 Thread.Sleep(3000); 48 Console.WriteLine($"子任务一,暂停3秒 {DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 49 return await Task.FromResult($"子任务一已完成,共计{str.Length}字节\r\n {DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 50 } 51 public async Task<string> ChildThreadTwo(string a, string b, string c) 52 { 53 string str = a + b + c; 54 Console.WriteLine($"子任务二:未暂停之前{DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 55 Thread.Sleep(12000); 56 Console.WriteLine($"子任务二,暂停7秒 {DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 57 return await Task.FromResult($"子任务二已完成,共计{str.Length}字节\r\n{DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 58 } 59 public async Task<string> ChildThreadThree() 60 { 61 Console.WriteLine($"子任务三::未暂停之前{DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 62 Thread.Sleep(4000); 63 Console.WriteLine($"子任务三:暂停4秒{DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 64 return await Task.FromResult($"子任务三已完成,{DateTime.Now.ToString("yyyy-HH-dd HH:mm:ss")}"); 65 } 66 }
以下是执行结果
Hello World! 开始启动线程开始时间2018-12-29 14:14:09 ******************未获取结果之前******************* 子任务三::未暂停之前2018-14-29 14:14:10 子任务一 未暂停之前2018-14-29 14:14:10 子任务二:未暂停之前2018-14-29 14:14:10 子任务一,暂停3秒 2018-14-29 14:14:13 子任务三:暂停4秒2018-14-29 14:14:14 子任务二,暂停7秒 2018-14-29 14:14:22 ========================函数内部=============================== 子任务一已完成,共计10字节2018-14-29 14:14:13 ========================函数内部=============================== ========================函数内部=============================== 子任务二已完成,共计16字节 2018-14-29 14:14:22 ========================函数内部=============================== ========================函数内部=============================== 子任务三已完成,2018-14-29 14:14:14 ========================函数内部=============================== 获取线程返回结果2018-12-29 14:14:22 子任务一已完成,共计10字节2018-14-29 14:14:13 获取线程返回结果2018-12-29 14:14:22 子任务二已完成,共计16字节 2018-14-29 14:14:22 获取线程返回结果2018-12-29 14:14:22 子任务三已完成,2018-14-29 14:14:14 主线程结束:2018-12-29 14:14:22
菜鸟随手记录!!