简体   繁体   中英

Parallel Programming : Process List of Dynamic Queues

I have a Dictionary of Items of different Types. The dictionary changes by adding new entries and items to existing entries.

What is the efficient way to process the records parallel. When Dictionary record 1 has 1 item and Dictionary record 2 has 200 items. When a new item is added to the Dictionary record 1 it should create a new thread and process it if old record is already processed instaed of waiting for the whole batch to complete.

Dictionary<ItemType, Queue<Item>> ItemsTypes = new Dictionary<Guid, Queue<Item>>();

With below code, I need to wait until the first batch of Items are processed before starting a new Batch.

result = Parallel.ForEach(ItemsTypes, Items => processor.ProcessItems(Items.Value));

What you are doing here with Parallel.ForEach is Data Parallelism. The moment this ForEach loop begins it takes a (kind of) snapshot of the current state of Dictionary and runs through it. I believe it doesnt matter if you add more items to any place once the forEach has started.

What you need to actually do is Pub/Sub model Where the code which adds new items/entries publishes an event which causes a new thread to be created and the item processed. Or you can add a Task object to each item.

Instead of Parallel.Foreach(), processed the Dictionary elements in a foreach and spin off new thread for each element and tracking statuses to prevent parallel processing of the Queue Items. Setting the Status to false inside the ProcessItems.

ConcurrentDictionary<Guid, bool> Status = new ConcurrentDictionary<Guid, bool>();

while (ItemsTypes.Values.Count() > 0)
{
    foreach (var item in ItemsTypes)
    {
      if (item.Value.Count > 0)
      {
        if (Status[issuer.Key] == false)
        {
          Status[issuer.Key] = true;
          Task.Run(() => ProcessItems(item.Value.Dequeue()));
         }
       }
     }
  }

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