Parallel.ForEach behavior
Parallel.ForEach behavior
this post is a direct continuation for the previous post about "Real-life story: Blocking Collection".
(Real-life story: Blocking Collection).ContinueWith (t => this post);
or
await (Real-life story: Blocking Collection);
this post;

my colleague Bram Veldhoen has suggest to demonstrate the behavior of the Parallel.ForEach thread's hunger in more pure fashion which doesn't include BlockingCollection<T> or any other high level Enumerable.
the following code demonstrate the issue by using a slow Enumerable:
Code Snippet
- private static int _getInt = 0;
- public static IEnumerable<int> GetInts()
- {
- Thread.Sleep(30 * 5000);
- yield return Interlocked.Increment(ref _getInt);
- }
-
- private static Timer _tmr;
- private static void Main()
- {
-
- _tmr = new Timer(state =>
- {
- var p = Process.GetCurrentProcess();
- int workerThreads, ioThreads;
- ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);
- Console.WriteLine("Thread Count = {0}, Available ThreadPool Threads = {1}",
- p.Threads.Count, workerThreads);
- }, null, 0, 1000);
- Parallel.ForEach(GetInts(), i => { });
- Console.ReadKey();
- }
when running this code we can see the following output:

we can see that thread count trend is constantly raising while the available ThreadPool's worker threads is declining.
you can read the previous post for solution to this issue.
Technorati Tags:
SELA,
Parallel,
Task,
Thread,
collection Windows Live Tags:
SELA,
Parallel,
Task,
Thread,
collection WordPress Tags:
SELA,
Parallel,
Task,
Thread,
collection Blogger Labels:
SELA,
Parallel,
Task,
Thread,
collection