Parallel.ForEach behavior

2012/02/27

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;

:DEV, SELA, Parallel, Task, Thread, TPL, async, TAP, paralle.foreach, pool, Blocking collection

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
  1. private static int _getInt = 0;
  2. public static IEnumerable<int> GetInts()
  3. {
  4.     Thread.Sleep(30 * 5000);
  5.     yield return Interlocked.Increment(ref _getInt);
  6. }
  7.  
  8. private static Timer _tmr;
  9. private static void Main()
  10. {
  11.  
  12.     _tmr = new Timer(state =>
  13.         {
  14.             var p = Process.GetCurrentProcess();
  15.             int workerThreads, ioThreads;
  16.             ThreadPool.GetAvailableThreads(out workerThreads, out ioThreads);
  17.             Console.WriteLine("Thread Count = {0}, Available ThreadPool Threads = {1}",
  18.                 p.Threads.Count, workerThreads);
  19.         }, null, 0, 1000);
  20.     Parallel.ForEach(GetInts(), i => { });
  21.     Console.ReadKey();
  22. }

when running this code we can see the following output:

:DEV, SELA, Parallel, Task, Thread, TPL, async, TAP, paralle.foreach, pool, Blocking collection

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: ,,,,
Windows Live Tags: SELA,Parallel,Task,Thread,collection
WordPress Tags: SELA,Parallel,Task,Thread,collection
Blogger Labels: SELA,Parallel,Task,Thread,collection

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. wordpress hosting2014/01/08 ב 13:21

    I am in fact thankful to the owner of this web site who has shared this
    enormous post at here.

    Reply