Task.Run() 和 Task.Factory.StartNew() 有什么区别 [英] What is the difference between Task.Run() and Task.Factory.StartNew()

查看:47
本文介绍了Task.Run() 和 Task.Factory.StartNew() 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有方法:

private static void Method(){Console.WriteLine("Method() 开始");for (var i = 0; i <20; i++){Console.WriteLine("Method() Counter = " + i);线程睡眠(500);}Console.WriteLine("Method() 完成");}

我想在一个新任务中启动这个方法.我可以像这样开始新任务

var task = Task.Factory.StartNew(new Action(Method));

或者这个

var task = Task.Run(new Action(Method));

但是Task.Run()Task.Factory.StartNew() 之间有什么区别吗?他们都在使用 ThreadPool 并在创建 Task 实例后立即启动 Method() .什么时候应该使用第一个变体,什么时候使用第二个?

解决方案

第二种方法 Task.Run 已在 .NET 框架的更高版本中引入(在 .NET 4.5 中).

然而,第一个方法,Task.Factory.StartNew,让你有机会定义很多关于你想要创建的线程的有用的东西,而 Task.Run代码>不提供这个.

例如,假设您要创建一个长时间运行的任务线程.如果线程池中的某个线程将用于此任务,则可以认为这是对线程池的滥用.

为了避免这种情况,您可以做的一件事是在单独的线程中运行任务.一个新创建的线程将专用于此任务,一旦您的任务完成,它将被销毁.无法使用 Task.Run,而您可以使用 Task.Factory.StartNew 来执行此操作,如下所示:

Task.Factory.StartNew(..., TaskCreationOptions.LongRunning);

正如此处所述:

<块引用>

因此,在 .NET Framework 4.5 开发人员预览版中,我们引入了新的 Task.Run 方法.这绝不是过时的 Task.Factory.StartNew,而应该简单地被认为是一种快速的使用方式Task.Factory.StartNew 无需指定一堆参数.是快捷方式. 其实Task.Run其实就是根据用于 Task.Factory.StartNew 的相同逻辑实现,只是传入一些默认参数.当你将一个 Action 传递给任务.运行:

Task.Run(someAction);

<块引用>

这完全等同于:

Task.Factory.StartNew(someAction,CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

I have Method :

private static void Method()
{
    Console.WriteLine("Method() started");

    for (var i = 0; i < 20; i++)
    {
        Console.WriteLine("Method() Counter = " + i);
        Thread.Sleep(500);
    }

    Console.WriteLine("Method() finished");
}

And I want to start this method in a new Task. I can start new task like this

var task = Task.Factory.StartNew(new Action(Method));

or this

var task = Task.Run(new Action(Method));

But is there any difference between Task.Run() and Task.Factory.StartNew(). Both of them are using ThreadPool and start Method() immediately after creating instance of the Task. When we should use first variant and when second?

解决方案

The second method, Task.Run, has been introduced in a later version of the .NET framework (in .NET 4.5).

However, the first method, Task.Factory.StartNew, gives you the opportunity to define a lot of useful things about the thread you want to create, while Task.Run doesn't provide this.

For instance, lets say that you want to create a long running task thread. If a thread of the thread pool is going to be used for this task, then this could be considered an abuse of the thread pool.

One thing you could do in order to avoid this would be to run the task in a separate thread. A newly created thread that would be dedicated to this task and would be destroyed once your task would have been completed. You cannot achieve this with the Task.Run, while you can do so with the Task.Factory.StartNew, like below:

Task.Factory.StartNew(..., TaskCreationOptions.LongRunning);

As it is stated here:

So, in the .NET Framework 4.5 Developer Preview, we’ve introduced the new Task.Run method. This in no way obsoletes Task.Factory.StartNew, but rather should simply be thought of as a quick way to use Task.Factory.StartNew without needing to specify a bunch of parameters. It’s a shortcut. In fact, Task.Run is actually implemented in terms of the same logic used for Task.Factory.StartNew, just passing in some default parameters. When you pass an Action to Task.Run:

Task.Run(someAction);

that’s exactly equivalent to:

Task.Factory.StartNew(someAction, 
    CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

这篇关于Task.Run() 和 Task.Factory.StartNew() 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆