Exemplo n.º 1
0
        static void ProducerConsumer()
        {
            Console.WriteLine("--- Producer Consumer ---");

            var queue = new CustomQueue <string>(1000);
            //var queue = new CustomQueueResetEvent<string>(1000);

            var producers = Enumerable
                            .Range(0, 4)
                            .Select(i => new Producer <string>(queue, () => $"{i} producer - {DateTime.Now.ToString()}"))
                            .Select(producer => new Thread(producer.StartProduce))
                            .ToList();
            var consumers = Enumerable
                            .Range(0, 2)
                            .Select(i => new Consumer <string>(queue))
                            .Select(producer => new Thread(producer.StartConsume))
                            .ToList();

            producers.ForEach(t => t.Start());
            consumers.ForEach(t => t.Start());

            Thread.Sleep(5000);

            producers.ForEach(p => p.Interrupt());
            consumers.ForEach(c => c.Interrupt());
        }
Exemplo n.º 2
0
 public Device(double _mu, CustomQueue queue, NextAction action)
 {
     NextAction = action;
     this.mu    = _mu;
     resetEvent = new AutoResetEvent(true);
     this.queue = queue;
 }
Exemplo n.º 3
0
 public Device(string name, double value, CustomQueue queue, NextAction action, WorkMode mode)
 {
     DeviceLoading = 0;
     isBusy        = false;
     Name          = name;
     NextAction    = action;
     this.value    = value;
     resetEvent    = new AutoResetEvent(true);
     this.queue    = queue;
     this.mode     = mode;
 }
Exemplo n.º 4
0
        public Source()
        {
            servedCustomers = new List <Customer>();

            settings = new Settings();
            queue1   = new CustomQueue();
            queue2   = new CustomQueue(QUEUE_2_LIMIT);
            device1  = new Device(DEVICE_1_MU, queue1, x => queue2.MoveToQueue(x));

            device2 = new Device(DEVICE_2_MU, queue2, x => servedCustomers.Add(x));
            device3 = new Device(DEVICE_3_MU, queue2, x => servedCustomers.Add(x));
        }
Exemplo n.º 5
0
        public void ExecuteAsync(CancellationToken stoppingToken)
        {
            void Init()
            {
                servedCustomers = new List <Customer>();

                queue1 = new CustomQueue(queue1Name, stoppingToken);
                queue2 = new CustomQueue(queue2Name, Settings.QUEUE_2_LIMIT, stoppingToken);

                helpQueue11 = new CustomQueue("HelpQueue1", 1, stoppingToken);
                helpQueue12 = new CustomQueue("HelpQueue2", 1, stoppingToken);

                device11 = new Device("Device1_1", Settings.DEVICE_1_MU, queue1, x => helpQueue11.MoveToQueue(x), WorkMode.Intensity);
                device12 = new Device("Device1_2", Settings.DEVICE_1_MU, queue1, x => helpQueue12.MoveToQueue(x), WorkMode.Intensity);
                device21 = new Device("Device2_1", Settings.DEVICE_2_MU, queue2, x => EndWork(x), WorkMode.Intensity);
                device22 = new Device("Device2_2", Settings.DEVICE_2_MU, queue2, x => EndWork(x), WorkMode.Intensity);

                helpDevice11 = new Device("HelpDevice", 0, helpQueue11, x => queue2.MoveToQueue(x), WorkMode.Time);
                helpDevice12 = new Device("HelpDevice", 0, helpQueue12, x => queue2.MoveToQueue(x), WorkMode.Time);
            }

            Init();

            var thread1 = new Thread(() => device11.ExecuteAsync(stoppingToken));

            thread1.Name = "Device11";

            var thread2 = new Thread(() => device12.ExecuteAsync(stoppingToken));

            thread2.Name = "Device12";

            var thread3 = new Thread(() => device21.ExecuteAsync(stoppingToken));

            thread3.Name = "Device21";

            var thread4 = new Thread(() => device22.ExecuteAsync(stoppingToken));

            thread4.Name = "Device22";

            var thread5 = new Thread(() => helpDevice11.ExecuteAsync(stoppingToken));

            thread5.Name = "HelpDevice11";
            var thread6 = new Thread(() => helpDevice12.ExecuteAsync(stoppingToken));

            thread6.Name = "HelpDevice12";

            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
            thread5.Start();
            thread6.Start();

            int delayTime = Settings.Delay;
            int count     = 0;

            while (!stoppingToken.IsCancellationRequested)
            {
                Customer customer = new Customer($"Customer {count}");
                customerCount++;

                customer.WriteToFile("Has been created");
                customer.CreateMessage("Has been created");

                queue1.MoveToQueue(customer);

                Thread.Sleep(delayTime);
                count++;
            }
        }
Exemplo n.º 6
0
        public void ExecuteAsync(CancellationToken stoppingToken)
        {
            void Init()
            {
                servedCustomers = new List <Customer>();

                queue1 = new CustomQueue(queue1Name, Settings.QUEUE_1_LIMIT, stoppingToken);
                queue2 = new CustomQueue(queue2Name, Settings.QUEUE_2_LIMIT, stoppingToken);

                helpQueue = new CustomQueue("HelpQueue", 1, stoppingToken);

                device1 = new Device("Device1", Settings.DEVICE_1_MU, queue1, x => helpQueue.MoveToQueue(x), WorkMode.Intensity);
                device2 = new Device("Device2", Settings.DEVICE_2_TIME, helpQueue, x => queue2.MoveToQueue(x), WorkMode.Time);
                device3 = new Device("Device3", Settings.DEVICE_3_TIME, queue2, x =>
                {
                    EndWork(x);
                }, WorkMode.Time);

                helpDevice = new Device("HelpDevice", 0, helpQueue, x => queue2.MoveToQueue(x), WorkMode.Time);
            }

            Init();

            var thread1 = new Thread(() => device1.ExecuteAsync(stoppingToken));

            thread1.Name = "Device1";

            var thread2 = new Thread(() => device2.ExecuteAsync(stoppingToken));

            thread2.Name = "Device2";

            var thread3 = new Thread(() => device3.ExecuteAsync(stoppingToken));

            thread3.Name = "Device3";

            var thread4 = new Thread(() => helpDevice.ExecuteAsync(stoppingToken));

            thread4.Name = "HelpDevice";

            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();

            int delayTime = Settings.Delay;
            int count     = 0;

            while (!stoppingToken.IsCancellationRequested)
            {
                Customer customer = new Customer($"Customer {count}");
                customerCount++;

                customer.WriteToFile("Has been created");
                customer.CreateMessage("Has been created");

                if (queue1.Count >= Settings.QUEUE_1_LIMIT)
                {
                    customer.WriteToFile("Has been rejected");
                    customer.CreateMessage("Has been rejected");
                    customer.IsRejected = true;

                    EndWork(customer);
                }
                else
                {
                    queue1.MoveToQueue(customer);
                }

                Thread.Sleep(delayTime);
                count++;
            }
        }