Parallel Programming-Multi-consumer, multi-producer concurrent operation, parallelprogramming

Source: Internet
Author: User

Parallel Programming-Multi-consumer, multi-producer concurrent operation, parallelprogramming

In the previous article, we demonstrated parallel pipeline operations (concurrently executed by producers and consumers). C # uses BlockingCollection, a thread-safe object, as a Buffer, and works with tasks. However, the previous article has a defect that the producer and consumer are the only one in the entire pipeline. This article demonstrates that multiple consumers and multiple producers execute concurrently.

I. Multiple consumers and multiple producers

Similar to the assembly line idea demonstrated in the previous article, the difference is that there are multiple topics in this article: consumers and producers. Taking buffer1 as an example, there are two producers and two consumers, now there are three latitudes in parallel:

Ii. Implement 2.1 code
 class PiplelineDemo    {        private int seed;        public PiplelineDemo()        {            seed = 10;        }        public void Action11(BlockingCollection<string> output)        {            for (var i = 0; i < seed; i++)            {                output.Add(i.ToString());//initialize data to buffer1            }        }        public void Action12(BlockingCollection<string> output)        {            for (var i = 0; i < seed; i++)            {                output.Add(i.ToString());//initialize data to buffer1            }        }        public void Action21(BlockingCollection<string> input, BlockingCollection<string> output)        {            foreach (var item in input.GetConsumingEnumerable())            {                var itemToInt = int.Parse(item);                output.Add((itemToInt * itemToInt).ToString());// add new data to buffer2            }        }        public void Action22(BlockingCollection<string> input, BlockingCollection<string> output)        {            foreach (var item in input.GetConsumingEnumerable())            {                var itemToInt = int.Parse(item);                output.Add((itemToInt * itemToInt).ToString());// add new data to buffer2            }        }        public void Action31(BlockingCollection<string> input, BlockingCollection<string> output)        {            foreach (var item in input.GetConsumingEnumerable())            {                output.Add((item));// add new data to buffer3            }        }        public void Action32(BlockingCollection<string> input, BlockingCollection<string> output)        {            foreach (var item in input.GetConsumingEnumerable())            {                output.Add((item));// add new data to buffer3            }        }        public void Pipeline()        {            var buffer1 = new BlockingCollection<string>(seed * 2);            var buffer2 = new BlockingCollection<string>(seed * 2);            var buffer3 = new BlockingCollection<string>(seed * 2);            var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);            var stage11 = taskFactory.StartNew(() => Action11(buffer1));            var stage12 = taskFactory.StartNew(() => Action12(buffer1));            Task.Factory.ContinueWhenAll(new Task[] { stage11, stage12 }, (tasks) =>            {                buffer1.CompleteAdding();            });            var stage21 = taskFactory.StartNew(() => Action21(buffer1, buffer2));            var stage22 = taskFactory.StartNew(() => Action22(buffer1, buffer2));            Task.Factory.ContinueWhenAll(new Task[] { stage21, stage22 }, (tasks) =>            {                buffer2.CompleteAdding();            });            var stage31 = taskFactory.StartNew(() => Action31(buffer2, buffer3));            var stage32 = taskFactory.StartNew(() => Action32(buffer2, buffer3));            Task.Factory.ContinueWhenAll(new Task[] { stage31, stage32 }, (tasks) =>            {                buffer3.CompleteAdding();            });            Task.WaitAll(stage11, stage12, stage21, stage22, stage31, stage32);            foreach (var item in buffer3.GetConsumingEnumerable())//print data in buffer3            {                Console.WriteLine(item);            }        }    }
2.2 running results

2.3 code explanation

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.