正确使用ParallelOptions,TaskCreationOptions和Task.Factory.StartNew?

时间:2013-07-26 10:53:28

标签: c# task-parallel-library

请建议哪种方法适当(如果有)正确和 有效使用ParallelOptions,TaskCreationOptions和 Task.Factory.StartNew(()=>。

private void NeedToUse_MaxDegreeOfParallelism_Method1()
{
    CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    //parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    task = Task.Factory.StartNew(() =>
    {
        while (!tokenFor_task.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    }, tokenFor_task.Token, tco, TaskScheduler.Default);
}


private void NeedToUse_MaxDegreeOfParallelism_Method2()
{
    //CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    task = Task.Factory.StartNew(() =>
    {
        while (!parOpts.CancellationToken.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    }, parOpts.CancellationToken, tco, parOpts.TaskScheduler);
}

private void NeedToUse_MaxDegreeOfParallelism_Method3()
{
    CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    //parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    task = Task.Factory.StartNew(() =>
    {
         Parallel.Invoke(parOpts, () =>
        //while is already in LongRunningMethod() because can not be here
        //while (!tokenFor_task.IsCancellationRequested) 
        //{
            LongRunningMethod()
        //}
        );
    }, tokenFor_task.Token, tco, TaskScheduler.Default);
}

private void NeedToUse_MaxDegreeOfParallelism_Method4()
{
    CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    //parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    Parallel.Invoke(parOpts, () =>
        task = Task.Factory.StartNew(() =>
        {
            while (!tokenFor_task.IsCancellationRequested)
            {
                LongRunningMethod();
            }
        }, tokenFor_task.Token, tco, TaskScheduler.Default)
    );
}

目前我没有收到任何错误。第一种和第二种方法没有考虑我需要使用的MaxDegreeOfParallelism。理想情况下,我不会使用Parallel.Invoke,但如何在Task.Factory.StartNew中包含parOpts.MaxDegreeOfParallelism?

1 个答案:

答案 0 :(得分:7)

您的代码和问题没有多大意义。 Task.Factory.StartNew()不接受MaxDegreeOfParallelism,因为它执行单一操作Parallel.Invoke()确实接受该参数,但是当您执行单个操作时使用该方法没有任何意义。

我没有问过这样一个非常具体的问题,我认为你应该退后一步,看看你实际想要达到的目标,然后可能会问一个新问题。

编辑:现在我想我终于明白了你要做的事情:在每个核心上,你想要执行一个单独的循环。为此,您可以使用Parallel.For()

Parallel.For(0, Environment.ProcessorCount, parOpts, () =>
    {
        while (!tokenFor_task.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    });