. Net (C #) TPL: Break method and lowestbreakiteration attribute of parallelloopstate

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.