zoukankan      html  css  js  c++  java
  • 多线程---Task理解(四)

    一、Task 是.NET Framwork 3.0 出现,线程是基于线程池的。

    二、创建Task的三种方式  

            public void TaskMethod()
            {
                Console.WriteLine($"TaskMethod 开始执行,ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}");
                //启动线程
                //1.Task task = new Task(() => Todo("TaskMethod"));
                //task.Start(); 
                {
                    Task task = new Task(() => Todo("Task"));
                    task.Start();
                }
                //2.Task.Run(() => Todo("TaskMethod"));
                {
                    Task.Run(() => Todo("Run"));
                }
                //3.Task.Factory.StartNew(() => Todo("TaskMethod"));
                {
                    Task.Factory.StartNew(() => Todo("StartNew"));
                }
                Console.WriteLine($"TaskMethod 开始执行,ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}");
            } 

    三、WaitAll  等待所有任务都完成了,在往下执行

           public void TaskWaitAll()
            {
                //WaitAll  等待所有任务都完成了,在往下执行
                List<Task> list = new List<Task>();
                for (int i = 0; i < 10; i++)
                {
                    var k = i;
                    var task = Task.Run(() =>
                     {
                         Thread.Sleep(500);
                         Console.WriteLine($"show is k ={k},ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                     });
                    list.Add(task);
                }
                Task.WaitAll(list.ToArray());
                Console.WriteLine("等待中..............");
                Console.WriteLine("等待中..............");
                Console.WriteLine("等待中..............");
                Console.WriteLine("WaitAll执行完了.....");
            } 

    四、TaskWaitAny  有一个任务执行完了,就往下执行

    public void TaskWaitAny()
            {
                //TaskWaitAny  有一个任务执行完了,就往下执行
                List<Task> list = new List<Task>();
                for (int i = 0; i < 10; i++)
                {
                    var k = i;
                    var task = Task.Run(() =>
                    {
                        Thread.Sleep(500);
                        Console.WriteLine($"show is k ={k},ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                    });
                    list.Add(task);
                }
                Task.WaitAny(list.ToArray());
                Console.WriteLine("等待中..............");
                Console.WriteLine("等待中..............");
                Console.WriteLine("等待中..............");
                Console.WriteLine("WaitAny执行完了.....");
            }

    五、WhenAll  等待所有任务都完成了,不阻塞的回调

            public void TaskWhenAll()
            {
                //WhenAll  等待所有任务都完成了,往下执行,不阻塞的回调
                List<Task> list = new List<Task>();
                for (int i = 0; i < 10; i++)
                {
                    var k = i;
                    var task = Task.Run(() =>
                    {
                        Thread.Sleep(1000);
                        Console.WriteLine($"show is k ={k},ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                    });
                    list.Add(task);
                }
                Task.WhenAll(list.ToArray()).ContinueWith(s =>
                {
                    Console.WriteLine("等待中..............");
                    Console.WriteLine("等待中..............");
                    Console.WriteLine("等待中..............");
                    Console.WriteLine($"TaskWhenAll执行完了,ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                });
            }

    六、WhenAny  等待所有任务都完成了,往下执行,不阻塞的回调

            public void TaskWhenAny()
            {
                //WhenAny  等待所有任务都完成了,往下执行,不阻塞的回调
                List<Task> list = new List<Task>();
                for (int i = 0; i < 10; i++)
                {
                    var k = i;
                    var task = Task.Run(() =>
                    {
                        Thread.Sleep(1000);
                        Console.WriteLine($"show is k ={k},ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                    });
                    list.Add(task);
                }
                Task.WhenAny(list.ToArray()).ContinueWith(s =>
                {
                    Console.WriteLine("等待中..............");
                    Console.WriteLine("等待中..............");
                    Console.WriteLine("等待中..............");
                    Console.WriteLine($"TaskWhenAny执行完了,ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                });
            }    

    七、控制并发数量只有20个

            public void ControlThreadNumber()
            {
                List<Task> tasks = new List<Task>();
                for (int i = 0; i < 10000; i++)
                {
                    var k = i;
                    if (tasks.Count(t => t.Status != TaskStatus.RanToCompletion) >= 20)
                    {
                        Task.WaitAny(tasks.ToArray());
                        tasks = tasks.Where(t => t.Status != TaskStatus.RanToCompletion).ToList();
                    }
                    var task = Task.Run(() =>
                    {
                        Thread.Sleep(500);
                        Console.WriteLine($"show is k ={k},ManagedThreadId={Thread.CurrentThread.ManagedThreadId}");
                    });
                    tasks.Add(task);
                }
            }

    八、Sleep和Delay的区别

      1.Sleep执行时间更长,Delay 执行时间段
      2.Sleep卡界面,阻塞,Delay 不卡界面,不阻塞
      3.同步等待,当前线程数等待2S后继续,异步等待,等待2S后启用新线程

         private void Todo(string name)
            {
                Console.WriteLine($"Todo 方法开始执行....");
    
                long result = 0;
                for (int i = 0; i < 100000; i++)
                {
                    result += i;
                }
                Thread.Sleep(500);
                Console.WriteLine($"Todo方法执行的结果:{result},名称{name},ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}");
    
                Console.WriteLine($"Todo 方法结束执行....");
            }
    将来的你,一定会感谢现在努力的自己!
  • 相关阅读:
    linux系统常用命令
    python文件处理
    Python按行读文件
    向脚本传递参数-shift命令
    shell脚本中一些特殊符号
    标准make变量 MAKE_VERSION, CURDIR
    makefile "=" ":=" "?=" "+="
    静态资源映射
    Spring MVC的常用注解
    SpringMVC项目的快速搭建
  • 原文地址:https://www.cnblogs.com/GreatPerson/p/14128772.html
Copyright © 2011-2022 走看看