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

I open nparallel threads in my function:

List<string> _files = new List<string>();

public void Start()
{
    CancellationTokenSource _tokenSource = new CancellationTokenSource();
    var token = _tokenSource.Token;

    Task.Factory.StartNew(() =>
    {
        try
        {
            Parallel.ForEach(_files,
                new ParallelOptions
                {
                    MaxDegreeOfParallelism = 5 //limit number of parallel threads 
                },
                file =>
                {
                    if (token.IsCancellationRequested)
                        return;
                    //do work...
                });
        }
        catch (Exception)
        { }

    }, _tokenSource.Token).ContinueWith(
        t =>
        {
            //finish...
        }
    , TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
    );
        }

After opening the threads, I noticed that he selects random files from my list. Can I select the first item nfrom my list each time ?

+3
source share
3 answers

To get the behavior you need, you need to write a custom delimiter , the reason it looks β€œrandom” right now, it gives the package a list of files in blocks, so if your original list was

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

, ( Max 3 ):

  • Thread1: "a", "b", "c"
  • 2: "d", "e", "f"
  • Thread3: "g", "h", "i"

, ,

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

, , ,

  • Thread1: "a", GetTheNextUnprocessedString()
  • Thread2: "b", GetTheNextUnprocessedString()
  • Thread3: "c", GetTheNextUnprocessedString()

.NET 4.5, factory :

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...
                });

.NET 4.5, , . MSDN, , .

, : " , ?" , , , , , , , , .

+4

Parallel.ForEach, , ; , , .

- 5 , . / , , , .

BlockingCollection 5 , :

var queue = new BlockingCollection<string>();
int workers = 5;
CancellationTokenSource cts = new CancellationTokenSource();
var tasks = new List<Task>();

for (int i = 0; i < workers; i++)
{
    tasks.Add(Task.Run(() =>
    {
        foreach (var item in queue.GetConsumingEnumerable())
        {
            cts.Token.ThrowIfCancellationRequested();

            DoWork(item);
        }
    }, cts.Token));
}

//throw this into a new task if adding the items will take too long
foreach (var item in data)
    queue.Add(item);
queue.CompleteAdding();

Task.WhenAll(tasks).ContinueWith(t =>
{
    //do completion stuff
});
+3

, , parallel.foreach. , 5 , partionier.

, OrderablePartitioner, parallel.foreach. β†’ http://msdn.microsoft.com/en-us/library/dd989583.aspx , , , , .

0

Source: https://habr.com/ru/post/1665889/


All Articles