简体   繁体   English

同步运行任务的目的是什么,何时执行?

[英]what is the purpose of running task synchronously and when to do it?

what is the purpose of running task synchronously 同步运行任务的目的是什么

public static void RunMeSync()
{
  for(int i = 0; i < 9999; i++)
  {
    ListBox1.Items.Add(i.ToString());
  }
}
public static void Main()
{
  Task T1 = new Task(()=>RunMeSync);
  T1.RunSynchronously();
  Task T2 = Task.Run(()=>RunMeSync);
}

is it pointless to do so ? 这样做毫无意义吗? what is the need to run task synchronously ? 同步运行任务需要什么?
Consider the following code snippet 考虑以下代码片段

public static void Main()
{
  Task T1 = new Task(()=>RunMeSync);
  T1.RunSynchronously();
  RunMeSync();
}

is it the same thing running T1 Synchronously, invoking RunMeSync directly? 同步运行T1并直接调用RunMeSync是同一件事吗?

Is it the same thing running T1 Synchronously, invoking RunMeSync directly? 同步运行T1 ,直接调用RunMeSync是否是同一件事?

Yes, the effect is going to be the same. 是的,效果将是相同的。 You would not gain anything by running synchronously a task constructed from a method that you could call directly. 通过同步运行由可以直接调用的方法构造的任务,您将不会获得任何收益。

However, not all tasks are like that: some are constructed from methods to which you have no access - for example, tasks passed into your class as parameters. 但是,并非所有任务都是这样:有些任务是由您无法访问的方法构造的,例如,作为参数传递给类的任务。 In other cases a task would have no named method associated with it, because it is constructed from a lambda or with an anonymous delegate. 在其他情况下,任务将没有与之关联的命名方法,因为它是由lambda或匿名委托构造的。 In all these cases, RunSynchronously() provides a way to calling the logic that implements the task, without knowing or worrying about the way in which that logic has been defined. 在所有这些情况下, RunSynchronously()提供了一种调用实现该任务的逻辑的方法,而无需了解或担心该逻辑的定义方式。

A Task itself is just a unit of work - and as-such, is agnostic to how it is run. Task本身只是一个工作单元,因此,它与运行方式无关。

It's entirely up to you to determine how it is run, based upon the needs, constraints and context of your program (albeit defaulting to potentially running in parallel) 根据程序的需求,约束和上下文(完全默认为可能并行运行),完全由您决定如何运行

The name RunSynchronously is a quite a bit of misnomer, better name would be TryToRunSynchronously . 名称RunSynchronously有点用词不当,更好的名称是TryToRunSynchronously Method will wait for the task to complete anyway irrespective of which thread it ran, maybe that's why they picked up this name. 无论运行哪个线程,方法都将等待任务完成,这也许就是他们选择此名称的原因。

Yes, as the name suggests Task will not be always run synchronously in current thread. 是的,顾名思义, Task不会总是在当前线程中同步运行。 If the underlying scheduler refuses to inline, Task will still be queued for execution and wait for it to complete. 如果基础调度程序拒绝内联,则Task仍将排队等待执行,并等待其完成。 TaskScheduler.TryExecuteTaskInline decides whether the Task is a candidate for inlining or not, another one parameter deciding the task inlining is the amount of stack available. TaskScheduler.TryExecuteTaskInline决定Task是否适合内联,另一个决定任务内联的参数是可用堆栈量。 So it is not same as calling the method synchronously. 因此,这不是一样的调用方法同步。

It might be helpful when you're dealing with some very deep recursive algorithms. 当您处理一些非常深的递归算法时,这可能会有所帮助。 Very deep recursion can cause StackOverflowException , you can check whether you have enough stack to proceed the recursion, if not just execute the Task in another thread (In fact that's how it is implemented). 非常深的递归会导致StackOverflowException ,您可以检查是否有足够的堆栈来进行递归,而不只是在另一个线程中执行Task(实际上就是这样实现的)。

Consider we are parallelizing some work. 考虑我们正在并行处理一些工作。 We split the work across threads and each of them does some deep recursion, If you're doing this with traditional recursive function, you'll end up with StackOverflowException . 我们将工作划分为多个线程,每个线程都进行一些深度递归。如果使用传统的递归函数执行此操作,则最终将得到StackOverflowException But if you wrap the recursive method logic inside Task.RunSynchronously then it will automatically stop recursing and queue the task to the underlying Scheduler and wait for it to complete if there is not enough stack available. 但是,如果将递归方法逻辑包装在Task.RunSynchronously则它将自动停止递归并将任务排队到基础Scheduler,并在没有足够堆栈可用时等待其完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM