简体   繁体   中英

Controlling number of threads using AsParallel or Parallel.ForEach

I have a huge collection, over which i have to perform a specific task(which involves calling a wcf service). I want to control the number of threads instead of using Parallel.ForEach directly. Here i have 2 options: I am using below to partition the data:

List<MyCollectionObject> MyCollection = new List<MyCollectionObject>();
 public static IEnumerable<List<T>> PartitionMyData<T>(this IList<T> source, Int32 size)
        {
            for (int i = 0; i < Math.Ceiling(source.Count / (Double)size); i++)
            {
                yield return new List<T>(source.Skip(size * i).Take(size));
            }
        }

Option 1:

MyCollection.PartitionMyData(AutoEnrollRequests.Count()/threadValue).AsParallel().AsOrdered()
                                        .Select(no => InvokeTask(no)).ToArray();

 private void InvokeTask(List<MyCollectionObject> requests)
{
   foreach(MyCollectionObject obj in requests)
  {
    //Do Something
  }
}

Option2:

MyCollection.PartitionMyData(threadValue).AsOrdered()
                                        .Select(no => InvokeTask(no)).ToArray();

private void InvokeTask(List<MyCollectionObject> requests)
{
    Action<MyCollectionObject> dosomething = 
    {
    }
    Parallel.ForEach(requests,dosomething)
}

If i have 16 objects in my collection, as per my knowledge Option1 will launch 4 threads, each thread having 4 objects will be processed synchronously. Option 2 will launch 4 threads with 1 object each, process them and again will launch 4 threads. Can anyone please suggest which option is better?

PS I understand .Net framework does thread pooling and we need not control the number of threads but due to some design decision we want to use it.

Thanks In Advance, Rohit

It's impossible to give an A or B answer here. It depends on too many unknowns.

I will assume you want the fastest approach. To see which is better, run both on the target environment (or closest approximation you can get) and see which one completes fastest.

I want to control the number of threads instead of using Parallel.ForEach directly

You can control de number of threads in Parallel.ForEach if you use this call with a ParallelOptions object:

Parallel.ForEach(requests,
                 new ParallelOptions(){MaxDegreeOfParallelism = 4}, //change here
                 dosomething)

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