简体   繁体   中英

Parallel.ForEach, Task.Factory and N threads

I know this has been asked and discussed before but still I think it is not clear what to use in this scenario.

I need to make 100 calls against a web service that I know can process 16 requests at a time (not that it would fail if it gets more)

I think that Parallel.ForEach is for more low level CPU intensive calls - it will decide how many threads are best suited. Task.Factory uses the thread pool so there could be less than 16 threads (also more)

Off course I could write my own Thread Pool, but isn't there a more obvious choice for this scenario?

I would use Parallel.ForEach but specify a ParallelOptions with MaxDegreeOfParallelism set to 16. That way you're giving a very clear indication of the degree of parallelism you want.

If you use Task.Factory I think it's reasonable to assume you will get more than 16 concurrent tasks, although if you use asynchronous IO you needn't have 16 concurrent threads anyway.

You might want to look at TPL Dataflow - I haven't looked specifically at how easily you can partition the consumers, but as it's aimed squarely at producer/consumer scenarios, I'd be surprised if you couldn't create 16 consumers, then just produce the 100 calls.

Or of course you could create 16 threads all consuming the same BlockingCollection containing data for the 100 calls. That would guarantee the level of parallelism...

Note that in order to actually make 16 calls at the same time to the same HTTP host, you may need to tweak the <connectionManagement> part of app.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM