Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ThreadPool.QueueUserWorkItem and Parallel.ForEach?

Tags:

What is the main difference between two of following approaches:

ThreadPool.QueueUserWorkItem

    Clients objClient = new Clients();     List<Clients> objClientList = Clients.GetClientList();      foreach (var list in objClientList)     {         ThreadPool.QueueUserWorkItem(new WaitCallback(SendFilesToClient), list);     }  

System.Threading.Tasks.Parallel ForEach

    Clients objClient = new Clients();     List<Clients> objClientList = Clients.GetClientList();      Parallel.ForEach<Clients>(objClientList, list =>     {         SendFilesToClient(list);     }); 

I am new to multi-threading and want to know what's going to happen in each case (in terms of execution process) what's the level of multi-threading for each approach? Help me visualize both the processes.

SendFilesToClient: Gets data from database, converts to Excel and sends the Excel file to respective client.

Thanks!

like image 263
Learner Avatar asked Feb 27 '13 21:02

Learner


People also ask

Does parallel ForEach use ThreadPool?

Parallel. ForEach uses managed thread pool to schedule parallel actions. The number of threads is set by ThreadPool.

What is ThreadPool QueueUserWorkItem?

QueueUserWorkItem(WaitCallback, Object) Queues a method for execution, and specifies an object containing data to be used by the method. The method executes when a thread pool thread becomes available.

What is the difference between thread and ThreadPool?

A thread pool is - as the name suggests - a pool of worker threads which are always running. Those threads then normally take tasks from a list, execute them, then try to take the next task. If there's no task, the thread will wait.

When should you not use ThreadPool?

Thread pools do not make sense when you need thread which perform entirely dissimilar and unrelated actions, which cannot be considered "jobs"; e.g., One thread for GUI event handling, another for backend processing. Thread pools also don't make sense when processing forms a pipeline.


1 Answers

The main difference is functional. Parallel.ForEach will block (by design), so it will not return until all of the objects have been processed. Your foreach queuing threadpool thread work will push the work onto background threads, and not block.

Also, the Parallel.ForEach version will have another major advantages - unhandled exceptions will be pushed back to the call site here, instead of left unhandled on a ThreadPool thread.

In general, Parallel.ForEach will be more efficient. Both options use the ThreadPool, but Parallel.ForEach does intelligent partitioning to prevent overthreading and to reduce the amount of overhead required by the scheduler. Individual tasks (which will map to ThreadPool threads) get reused, and effectively "pooled" to lower overhead, especially if SendFilesToClient is a fast operation (which, in this case, will not be true).

Note that you can also, as a third option, use PLINQ:

objClientList.AsParallel().ForAll(SendFilesToClient); 

This will be very similar to the Parallel.ForEach method in terms of performance and functionality.

like image 113
Reed Copsey Avatar answered Sep 19 '22 07:09

Reed Copsey