Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to process queue with N threads in C#?

I have a list of account numbers. Foreach account number I need to call method ProcessAccount. There will be approximately 150,000 accounts that need to be processed and each account could take between .5 to 2 seconds to process.

I'd like to setup threading somehow so I can be processing 4 accounts at a time for example.

Is there a simple pattern I can use for this?

What I'd like to be able to do is start 4 threads processing the first 4 accounts, and then as each individual thread is finished start another thread with the next account until all the accounts have been processed.

like image 674
AWeim Avatar asked Sep 11 '12 18:09

AWeim


People also ask

What is a queue in C++?

A queue is a FIFO (First-In, First-Out) data structure in which the element that is inserted first is the first one to be taken out. The elements in a queue are added at one end called the REAR and removed from the other end called the FRONT. Queues can be implemented by using either arrays or linked lists.

How many threads does a processqueue start out with?

By default, the ProcessQueue starts out with one worker thread. This is suitable for asynchronous processing of the items in the ProcessQueue when the time to complete is not as important as conserving resources, or when it’s essential that items in the ProcessQueue be processed in the same order as they were entered.

How to share a queue among three threads in Java?

Share a queue among three threads A, B, C as per given norms : Thread A generates random integers and pushes them into a shared queue. Threads B and C compete with each other to grab an integer from the queue. The threads B and C compute the sum of integers that they have grabbed from the queue.

How do threads compare the sum of integers in a queue?

The threads B and C compute the sum of integers that they have grabbed from the queue. Compare the sums as computed by B and C. The greatest is the winner.


2 Answers

This is easy to handle with the TPL (Task Parallel Library). It would look something like

ParallelOptions options = new ParallelOptions() { MaxDegreeOfParallelism = 4 };
Parallel.ForEach(accounts, options, a =>
{
    ProcessAccount(a);
});

http://msdn.microsoft.com/en-us/library/dd782721.aspx

Note that the TPL might decide to run less than 4 concurrent threads, but will not run more than 4 based on the specified options. It might do that, for example, if it determines that the provided lamda (which calls ProcessAccount) is CPU bound and there are less than 4 CPU cores on the system. Generally, especially in .NET 4.5, the TPL makes very good decisions about the number of threads to use.

As @Servy notes in the comments, unless you have a very specific reason to limit the code to 4 threads, it is best to just let TPL sort out how many threads to use on its own. That way, if the same code is running on a 128 core processor in the year 2018, long after you move on to other things, it is free to use all 128 cores).

like image 186
Eric J. Avatar answered Sep 20 '22 02:09

Eric J.


Use PLinq:

var accounts = //some 150,000 account numbers
accounts.AsParallel().ForAll(ProcessAccount);

or, if other arguments are required, use a lambda expression:

accounts.AsParallel().ForAll(account => ProcessAccount(account, argument2, argument3));
like image 40
phoog Avatar answered Sep 23 '22 02:09

phoog