Can Task MaxDegreeOfParallelism can take every time the first n object from my list?

后端 未结 3 1167
执笔经年
执笔经年 2021-01-14 03:31

I am opening n concurrent threads in my function:

List _files = new List();

public void Start()
{
    Cancellation         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 03:56

    To get the behavior you want you need to write a custom partitioner, The reason it looks "random" is right now it is batching out the file list in blocks so if your source list was

    List files = List { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
    

    when it partitions it it may split it evenly like so (if Max was 3 threads):

    • Thread1's work list: "a", "b", "c"
    • Thread2's work list: "d", "e", "f"
    • Thread3's work list: "g", "h", "i"

    So if you watched the files being processed it may look like

    "a", "d", "g", "e", "b", "h", "c", "f", "i"
    

    If you make a custom partitioner you can have it take one item at a time instead of a batch at a time to make the work list look like

    • Thread1's work list: "a", GetTheNextUnprocessedString()
    • Thread2's work list: "b", GetTheNextUnprocessedString()
    • Thread3's work list: "c", GetTheNextUnprocessedString()

    If you are using .NET 4.5 you can use this factory like so:

    Parallel.ForEach(Partitioner.Create(_files, EnumerablePartitionerOptions.NoBuffering),
                    new ParallelOptions
                    {
                        MaxDegreeOfParallelism = 5 //limit number of parallel threads 
                    },
                    (file, loopstate, index) =>
                    {
                        if (token.IsCancellationRequested)
                            return;
                        //do work...
                    });
    

    If you are not using .NET 4.5, it is not a trivial task so I am not going to write it here for you. Read the MSDN article I linked at the top and you will be able to figure it out eventually.

    What I would do is ask yourself "do I really need the files to be processed in order?" if you don't need them to be in order let it do its own ordering as the only thing you will likely do by enforcing a order is potentially slowing down the process.

提交回复
热议问题