简体   繁体   中英

Can I nest Parallel.Invoke methods?

I was wondering whether it's possible or recommended to have multiple layers of the Parallel.Invoke method or whether it is likely to cause problems / slow the application.

Is there a best practice for this?

Parallel.Invoke(
    () => Invoke("orange"),
    () => Invoke("apples"),
    () => Invoke("pears")
);           

public static void Invoke(string name)
{
    Parallel.Invoke(
        () => test.testapp(name),
        () => test.testapp(name),
        () => test.testapp(name)
    );
}

It's possible, but there might be no benefit - it really depends on what those jobs are doing.

It might be beneficial if they're essentially asynchronous tasks (eg on the database or external API) which can be executed in parallel. However, if they're CPU-intensive tasks then you're probably just going to give extra work to the scheduler for no real benefit.

There's an article here which contains some benchmarks using the Parallel libary, it might give you a good idea when to use this.

Results of this test:

Simulating calling external resources:

Not Nested: 00:00:14.6155628
Nested: 00:00:09.0907248

CPU intensive tasks:

Not Nested: 00:00:05.6169188
Nested: 00:00:06.1500767

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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