Exemplo n.º 1
0
        void cashierServesCustomer(Cashier cashier, int customerId)
        {
            int servingTime = (int)((servingTimeMin + rnd.NextDouble() * (servingTimeMax - servingTimeMin)) * 1000);

            ThreadPool.QueueUserWorkItem(obj =>
            {
                cashier.serve(customerId, servingTime);
                cashier.IdleEvent.Set();
            });
        }
Exemplo n.º 2
0
 static void Main(string[] args)
 {
     //创建收银对象,从而创建仓库
     Cashier cs = new Cashier();
     //展示货物
     cs.ShowPros();
     //与顾客交互
     cs.WhatToBuy();
     Console.ReadKey();
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var token = cancellationTokenSource.Token;

            // create and init init Cashiers & QueueManager
            var cashiers = new List <ICashier>(Config.NumberOfCashiers);

            for (int i = 0; i < Config.NumberOfCashiers; i++)
            {
                var cashier = new Cashier(i, Constants.ONE_SEC * Config.OrderProcessingMinTimeInSec, Constants.ONE_SEC * Config.OrderProcessingMaxTimeInSec);
                cashiers.Add(cashier);
            }

            var queueManager = new QueueManager(cashiers);

            // start the Queue Manager
            queueManager.Start(cancellationTokenSource);


            // generate new customers
            int peopleJoinQueueRate = Config.PeopleJoinQueueRatePerSecond * Constants.ONE_SEC;

            var addCustomersTask = new Task(async() =>
            {
                while (!token.IsCancellationRequested)
                {
                    // add new customers to the queue by defined rate
                    await Task.Delay(peopleJoinQueueRate);

                    queueManager.AddCustomer(new Customer());
                }
            }, token, TaskCreationOptions.LongRunning);

            // start generating customers
            addCustomersTask.Start();


            Console.WriteLine("Type \"quit\" to exit.\r\n");

            // exit application
            var input = string.Empty;

            while (input.ToLower() != "quit")
            {
                input = Console.ReadLine();
            }

            cancellationTokenSource.Cancel();
        }
Exemplo n.º 4
0
        public QueueHandler(int numOfCashiers, double servingMin, double servingMax)
        {
            for (int i = 0; i < numOfCashiers; ++i)
            {
                Cashier cashier = new Cashier(i + 1);
                cashiers.Add(cashier);
            }

            servingTimeMin = servingMin;
            servingTimeMax = servingMax;

            Thread t = new Thread(ThreadFunc);

            t.Start();
        }