Easy way to handle a queue with N threads in C #?

I have a list of account numbers. Front account number I need to call the ProcessAccount method. About 150,000 accounts will be processed, and each account may take from 0.5 to 2 seconds to process.

I would like to set up the threads anyway so that I can process 4 accounts at a time.

Is there a simple template that I can use for this?

What I would like to do is start 4 threads processing the first 4 accounts, and then at the end of each individual thread, start another thread with the next account until all accounts are processed.

+7
source share
2 answers

This is easy to handle using TPL (parallel task library). It would look like

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

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

Please note that TPL may decide to start less than 4 simultaneous threads, but will not work more than 4 based on the specified options. He can do this, for example, if he determines that the provided lamda (which calls ProcessAccount) is connected to the CPU and the system has less than 4 processor cores. Typically, especially in .NET 4.5, TPL makes very good decisions about the number of threads used.

As @Servy notes in the comments, if you don’t have any special reason to limit the code to 4 threads, it’s best to just let TPL sort how many threads to use on your own. Thus, if the same code runs on a 128-core processor in 2018, after you move on to other things, it can use all 128 cores).

+12
source

Use PLinq:

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

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

 accounts.AsParallel().ForAll(account => ProcessAccount(account, argument2, argument3)); 
+1
source

All Articles