简体   繁体   中英

Parallel For loops. Are they wait for finish?

I have two for loops. which the second loop must be started after finishing the first loop .

So, If I use two Parallel.For() loops, will the second loop runs after the finishing the first loop?

Yes. Parallel.For will not return until all operations have completed.

If you run

Parallel.For(0, 5, i => Console.WriteLine("First {0}", i));
Console.WriteLine("First Finished");
Parallel.For(0, 5, i => Console.WriteLine("Second {0}", i));
Console.WriteLine("Second Finished");

The output is

First 0
First 2
First 1
First 4
First 3
First Finished
Second 0
Second 4
Second 3
Second 2
Second 1
Second Finished

The order of the integers will vary, but second will always come after first.

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