First, when the break is called,There is no need to execute the iteration after the break is called in the loop.Because parallel execution breaks down the execution order, it is very likely that some iterations that do not need to be executed have been completed, and this cannot be canceled (or necessary, of course, after the break is called, the iteration that does not need to be executed and is not started will not be executed.
The above only discusses the "executed" and "status without execution". For an iteration that is being executed but does not need to be executed, it cannot be stopped. At this time, parallelloopstate. lowestbreakiteration (type: long ?, The default value is null.
Let's look at the first example. We use the 1-20 command to iterate 20 times, but if it is equal to 1, the break will be called soon because it is 1. Similarly, because no specific code is executed for each iteration, there is no "this situation does not occur when no iteration is being executed ".
Code:
// Count execution times
Int COUNT = 0;
Parallel. For (1, 21, (I, state) =>
{
// Output: number of iterations, thread ID and lowestbreakiteration
Console. writeline ("Iteration {0} thread {1} lowestbreakiteration {2 }",
I,
Thread. currentthread. managedthreadid,
State. lowestbreakiteration );
If (I = 1)
State. Break ();
Interlocked. increment (ref count );
});
Console. writeline ("executions: {0}", count );
My computer output:
Iteration 1 thread 1 lowestbreakiteration
Iteration 6 thread 3 lowestbreakiteration
Iteration 11 thread 4 lowestbreakiteration
Iteration 16 thread 5 lowestbreakiteration
Iteration 2 thread 6 lowestbreakiteration
Number of executions: 5
We can see that there are five independent threads performing iteration of 1, 6, 11, 16, and 2, where only 1 is required, the execution of other iterations is because the break call is unknown during parallel execution. After the break is interrupted, all the iterations that are not necessary to be executed are not executed, and only five times are executed in total!
Finally, lowestbreakiteration is always null because the iteration execution time is short and there is no "unnecessary iteration in progress.
The second example is the same as the previous one, except that thread. Sleep is added to iterative execution to simulate time-consuming work. In this way, there will be "iterations not required for execution", that isWhen an execution thread is working, it finds that it is unnecessary to run it.!
Code:
// Count execution times
Int COUNT = 0;
Parallel. For (1, 21, (I, state) =>
{
// Simulate the work
Thread. Sleep (700 );
// Output: number of iterations, thread ID and lowestbreakiteration
Console. writeline ("Iteration {0} thread {1} lowestbreakiteration {2 }",
I,
Thread. currentthread. managedthreadid,
State. lowestbreakiteration );
If (I = 1)
State. Break ();
Interlocked. increment (ref count );
});
Console. writeline ("executions: {0}", count );
I got the following output:
Iteration 6 thread 3 lowestbreakiteration
Iteration 16 thread 5 lowestbreakiteration
Iteration 2 thread 6 lowestbreakiteration
Iteration 1 thread 1 lowestbreakiteration
Iteration 11 thread 4 lowestbreakiteration
Iteration 3 thread 6 lowestbreakiteration 1
Iteration 17 thread 5 lowestbreakiteration 1
Iteration 7 thread 3 lowestbreakiteration 1
Execution times: 8
When the break method is executed in 3, 17, and 7, lowestbreakiteration is the minimum number of iterations for calling break, which is 1 in this example.