Consider the following code:
ConcurrentBag<string> results = new ConcurrentBag<string>();
Parallel.ForEach(stringArray, myString =>
{
if(myString.Contains("hello")){
results.Add(myString);
}
});
Console.WriteLine(String.Concat(results));
where stringArray
is an array of hundreds of thousands of strings and 20 of them contain the word "hello" (they are evenly distributed). No matter how many times I execute the code, the output is always the same. I expected the various outputs to be somewhat different from one another as the order of execution of each ForEach is not guaranteed, so why am I obtaining always the same result? Is this normal? I'm wondering if the execution is really split in multiple threads.
For context I am on Windows 10 with a 8-cores, 16-threads CPU. I tried to increase MaxDegreeOfParallelism
still without any difference.