Example #1
0
        private Resource CreateBarbers()
        {
            Barber[] barbers = new Barber[4];
            barbers[0] = new Barber(this, "Frank");
            barbers[1] = new Barber(this, "Tom");
            barbers[2] = new Barber(this, "Bill");
            barbers[3] = new Barber(this, "Joe");

            return(Resource.Create(barbers));
        }
        private void startButton_Click(object sender, EventArgs e)
        {
            GlobalVariable glv = new GlobalVariable();

            Customer[] myCustomer = new Customer[50];
            Barber[]   myBarber   = new Barber[3];
            Cashier    myCashier  = new Cashier();

            for (int i = 0; i < 50; i++)
            {
                myCustomer[i] = new Customer(this);
            }
            for (int i = 0; i < 3; i++)
            {
                myBarber[i] = new Barber();
            }
            Thread[] customerThreads = new Thread[50];
            Thread[] barberThreads   = new Thread[3];
            Thread   cashierThread   = new Thread(new ThreadStart(myCashier.CashierFunction));

            Thread[]  firstRunThread = new Thread[GlobalVariable.numberOfCustomer];
            Objects[] myObjects      = new Objects[GlobalVariable.numberOfCustomer];
            for (int i = 0; i < GlobalVariable.numberOfCustomer; i++)
            {
                myObjects[i]            = new Objects(this);
                firstRunThread[i]       = new Thread(new ThreadStart(myObjects[i].Run));
                firstRunThread[i].Name  = (i + 1).ToString();
                customerThreads[i]      = new Thread(new ThreadStart(myCustomer[i].CustomerFunction));
                customerThreads[i].Name = (i + 1).ToString();
            }
            for (int i = 0; i < 3; i++)
            {
                barberThreads[i] = new Thread(new ThreadStart(myBarber[i].BarberFunction));
            }
            for (int i = 0; i < GlobalVariable.numberOfCustomer; i++)
            {
                firstRunThread[i].Start();
                Thread.Sleep(1);
            }
            for (int i = 0; i < GlobalVariable.numberOfCustomer; i++)
            {
                customerThreads[i].Start();
                Thread.Sleep(800);
                if (i < 3)
                {
                    barberThreads[i].Start();
                    if (i == 0)
                    {
                        cashierThread.Start();
                    }
                }
            }
        }
Example #3
0
        protected override IEnumerator <Task> GetProcessSteps()
        {
            Resource barbers = (Resource)ActivationData;

            yield return(barbers.Acquire(this));

            System.Diagnostics.Debug.Assert(barbers == Activator);
            System.Diagnostics.Debug.Assert(ActivationData != null);
            Barber barber = (Barber)ActivationData;

            WaitOnTask(barber);
            yield return(Suspend());

            // HINT: The above two lines of code can be shortened to
            //          yield return barber;

            Console.WriteLine("Customer pays {0} for the haircut.",
                              barber.Name);

            yield return(barbers.Release(this));
        }
Example #4
0
 private void AvailableBarber(Barber barber)
 {
     barber.AssignCustomer(FetchCustomerForCutting());
 }
Example #5
0
 private void BarberVacancy(Barber barber)
 {
     Console.WriteLine($"Reporter: Barber #{barber.Id} is free.");
 }
Example #6
0
        protected override IEnumerator<Task> GetProcessSteps()
        {
            Resource barbers = (Resource)ActivationData;
            //NewResource barbers = (NewResource)ActivationData;
            Console.WriteLine("M         so Cus trong hang doi = " + barbers.BlockCount + " " + Now);
            //if (barbers.BlockCount < QueueCapacity)//max so Cus trong hang doi
            //{

            //    yield return barbers.Acquire(this);//?o?n n?y s? nh?y sang Barber ?? th?c hi?n, khi th?c hi?n xong s? nh?y v? 2//Sau doan nay Cus se luu vao hang doi
            //}
            //else
            //{
            //    yield break;
            //}

            if (barbers.BlockCount < Condition)//max so Cus trong hang doi
            {
                yield break;

            }
            else
            {               
                yield return barbers.Acquire(this);//?o?n n?y s? nh?y sang Barber ?? th?c hi?n, khi th?c hi?n xong s? nh?y v? 2//Sau doan nay Cus se luu vao hang doi
            }


            System.Diagnostics.Debug.Assert(barbers == Activator);
            System.Diagnostics.Debug.Assert(ActivationData != null);
            Barber barber = (Barber)ActivationData;
            Points.Add(new Point(Now, barber.BlockCount));
            Console.WriteLine("H  H      so Cus trong hang doi = " + barbers.BlockCount + " " + Now);

            TimeIn = this.Now;
            Console.WriteLine(this.Now + " ? " + barber.Name + " begins cutting hair of customer " + this.Name);

            WaitOnTask(barber);
            yield return Suspend();
            // HINT: The above two lines of code can be shortened to
            //          yield return barber;

            Console.WriteLine("NN  NN    so Cus trong hang doi = " + barbers.BlockCount + " " + Now);

            TimeOut = this.Now;
            Console.WriteLine(this.Now + " x Customer " + Name + " pays {0} for the haircut.",
                barber.Name);
            Console.WriteLine($"thoi gian trong hang doi {TimeIn - this.TimeCome}" +
                              $" --- thoi gian trong he thong {TimeOut - this.TimeCome}");

            yield return barbers.Release(this);//giai phong bo nho

            //Resource barbers = (Resource)ActivationData;
            //yield return barbers.Acquire(this);

            //System.Diagnostics.Debug.Assert(barbers == Activator);
            //System.Diagnostics.Debug.Assert(ActivationData != null);
            //Barber barber = (Barber)ActivationData;

            //WaitOnTask(barber);
            //yield return Suspend();
            //// HINT: The above two lines of code can be shortened to
            ////          yield return barber;

            //Console.WriteLine("Customer pays {0} for the haircut.",
            //    barber.Name);

            //yield return barbers.Release(this);
        }