Beispiel #1
0
        private static void SpawnAlchemists(AlchemistsIndulger indulger)
        {
            for (int i = 0; i < 100; i++)
            {
                int       rand      = random.Next(4);
                Alchemist alchemist = null;

                switch (rand)
                {
                case 0:
                    alchemist = new AlchemistA();
                    break;

                case 1:
                    alchemist = new AlchemistB();
                    break;

                case 2:
                    alchemist = new AlchemistC();
                    break;

                case 3:
                    alchemist = new AlchemistD();
                    break;
                }

                Thread thread = new Thread(() =>
                {
                    alchemist.RequestResources(indulger);
                });
                thread.IsBackground = true;
                thread.Start();
            }
        }
        public void ResourceRequestedBy(Alchemist alchemist)
        {
            bool isFirst = false;

            if (alchemist is AlchemistA)
            {
                aQueueSem.Wait();
                isFirst = AlchemistsA.Any();
                AlchemistsA.Enqueue(alchemist);
                aQueueSem.Release();
            }
            else if (alchemist is AlchemistB)
            {
                bQueueSem.Wait();
                isFirst = AlchemistsB.Any();
                AlchemistsB.Enqueue(alchemist);
                bQueueSem.Release();
            }
            else if (alchemist is AlchemistC)
            {
                cQueueSem.Wait();
                isFirst = AlchemistsC.Any();
                AlchemistsC.Enqueue(alchemist);
                cQueueSem.Release();
            }
            else if (alchemist is AlchemistD)
            {
                dQueueSem.Wait();
                isFirst = AlchemistsD.Any();
                AlchemistsD.Enqueue(alchemist);
                dQueueSem.Release();
            }

            if (isFirst)
            {
                TryDistributeResources();
            }
        }