ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#指定TaskCreationOptions来创建Task

2022-01-27 06:00:16  阅读:129  来源: 互联网

标签:Task Console C# TaskCreationOptions task 线程 WriteLine


在使用Task.Factory.StartNew或者Task.Factory.FromAsync方法创建任务时,一些重载方法允许提供TaskCreationOptions来向调度器提示任务的调度方案。这里简要介绍了AttachedToParent、DenyChildAttach、HideScheduler、LongRunning、PreferFairness五种选项的具体行为。


AttachedToParent

在一个Task中创建另一个Task时,爸爸Task通常不会等待儿子Task结束。例如:

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var parent = Task.Factory.StartNew(() => {
         Console.WriteLine("Outer task executing.");

         var child = Task.Factory.StartNew(() => {
            Console.WriteLine("Nested task starting.");
            Thread.SpinWait(500000);
            Console.WriteLine("Nested task completing.");
         });
      });

      parent.Wait();
      Console.WriteLine("Outer has completed.");
   }
}
// The example produces output like the following:
//        Outer task executing.
//        Nested task starting.
//        Outer has completed.
//        Nested task completing.
View Code

相应地,使用TaskCreationOptions.AttachedToParent创建的儿子Task则带有以下3个特点(这3个特点也是默认情况下创建的儿子Task不具备的):

  1. 爸爸Task会等待儿子Task结束。
  2. 爸爸Task会捕获儿子Task的Exception。
  3. 爸爸Task的执行状态取决于儿子Task的执行状态。

例如,上面的代码使用TaskCreationOptions.AttachedToParent,则会得到以下的输出:

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var parent = Task.Factory.StartNew(() => {
            Console.WriteLine("Parent task executing.");
            var child = Task.Factory.StartNew(() => {
                  Console.WriteLine("Attached child starting.");
                  Thread.SpinWait(5000000);
                  Console.WriteLine("Attached child completing.");
            }, TaskCreationOptions.AttachedToParent);
      });
      parent.Wait();
      Console.WriteLine("Parent has completed.");
   }
}
// The example displays the following output:
//       Parent task executing.
//       Attached child starting.
//       Attached child completing.
//       Parent has completed.

DenyChildAttach

如果你不希望一个Task的启动的儿子们Attach到它自己身上,则可以在启动爸爸Task时为它指定TaskCreationOptions.DenyChildAttach。当通过DenyChildAttach启动的爸爸Task试图指定AttachedToParent来启动儿子Task时,AttachedToParent将会失效。


HideScheduler

当指定TaskCreationOptions.HideScheduler时,创建Task里再创建的儿子Task将使用默认的TaskScheduler,而不是当前的TaskScheduler。这相当于在创建Task时隐藏了自己当前的TaskScheduler。对于本身就是在默认的TaskScheduler里创建的Task,这个选项似乎没什么用。


LongRunning

C#启动的Task都会通过TaskScheduler来安排执行。根据官方文档的描述:

The default scheduler for the Task Parallel Library and PLINQ uses the .NET Framework thread pool, which is represented by the ThreadPool class, to queue and execute work. The thread pool uses the information that is provided by the Task type to efficiently support the fine-grained parallelism (short-lived units of work) that parallel tasks and queries often represent.

而默认的TaskScheduler采用的是.NET线程池ThreadPool,它主要面向的是细粒度的小任务,其执行时间通常在毫秒级。线程池中的线程数与处理器的内核数有关,如果线程池中没有空闲的线程,那么后续的Task将会被阻塞。因此,如果事先知道一个Task的执行需要较长的时间,就需要使用TaskCreationOptions.LongRunning枚举指明。使用TaskCreationOptions.LongRunning创建的任务将会脱离线程池启动一个单独的线程来执行。


PreferFairness

提示TaskScheduler以一种尽可能公平的方式安排任务,这意味着较早安排的任务将更可能较早运行,而较晚安排运行的任务将更可能较晚运行。

转载自:https://www.licc.tech/article?id=53

标签:Task,Console,C#,TaskCreationOptions,task,线程,WriteLine
来源: https://www.cnblogs.com/cdaniu/p/15848633.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有