Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7654123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:11:56+00:00 2026-05-31T12:11:56+00:00

I got an assignment that I find it quite challenging and I have no

  • 0

I got an assignment that I find it quite challenging and I have no idea how to start.

Here is the question:

I got a queue of random Products(consists of type A,B and C), says

P[A], P[A], P[B], P[C], P[C], P[A], P[B], P[B]….

I gonna send these product to ProcessProduct() concurrently but two same type of products can not be processed together.

Here is the scenario:

first P[A] get processed and the second P[A] will have to wait for the first one, and the third P[B] and the fourth P[C] will get processed right away, and the fifth P[C] will have to wait for the previous one….

Hope my description is not too confusing.
I will appreciate it if anyone could give me some suggestions.
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:11:57+00:00Added an answer on May 31, 2026 at 12:11 pm

    Group your product list by type then process each group in parallel.

    EDITED

    This an example (if the product list is static):

    sealed class Product
    {
        public string Type
        {
            get;
            set;
        }
    }
    
    sealed class ProductProcessor
    {
        public void StartProcessing(IEnumerable<Product> products)
        {
            foreach (var group in products.GroupBy(x => x.Type))
                Task.Factory.StartNew(() => ProcessProducts(group));
        }
    
        private void ProcessProducts(IEnumerable<Product> products)
        {
            foreach (Product product in products)
                ProcessProduct(product);
        }
    
        private void ProcessProduct(Product product)
        {
    
        }
    }
    

    This is another example for a dynamic queue of products to process (I didn’t run the code, read it as a kind of pseudo-code):

    sealed class ProductProcessor
    {
        public void Process(Product product)
        {
            lock (_queues)
            {
                if (_queues.ContainsKey(product.Type))
                    _queues[product.Type].Enqueue(product);
                else
                {
                    ConcurrentQueue<Product> queue = new ConcurrentQueue<Product>();
                    queue.Enqueue(product);
    
                    _queues.Add(product.Type, queue);
    
                    WaitCallback action = delegate(object state)
                    {
                        Product productToProcess;
                        while (queue.TryDequeue(out productToProcess))
                        {
                            ProcessProduct(productToProcess);
                        }
    
                        lock (_queues) _queues.Remove(product.Type);
                    };
    
                    ThreadPool.QueueUserWorkItem(action);
                }
            }
        }
    
        private Dictionary<string, ConcurrentQueue<Product>> _queues
            = new Dictionary<string, ConcurrentQueue<Product>>();
    
        private void ProcessProduct(Product product)
        {
    
        }
    }
    

    P.S. Instead of the lock you may use a ConcurrentDictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment problem that i can't manage to fix and got totally
I have got this assignment question on HMM and I have solved it. I
I got an assignment that i have to make a programm in Java that
I got this question as an assignment: Packages/Naming We have created lot of packages
This is part of a homework assignment. I've got several questions asking find the
I have an assignment and i got a library including an interface class. [InfoItem]
I've got an assignment to write a server program that accepts multiple clients. I'm
I got the following assignment - I have a certain java code for a
I've got a homework assignment where I have a base class Package, and two
I got an assignment where I have to implement a custom call forwarding system

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.