Пример #1
0
        private static void TestGenericPool()
        {
            ConsoleWriter.Instance.WriteEmphasis("Generic Pool Test\n");

            IGenericObjectPool <TestPoolItem> testPool = new GenericObjectPool <TestPoolItem>(10, BalancingMethods.RoundRobin);

            List <TestPoolItem> items = new List <TestPoolItem>();

            for (var i = 0; i < 5; i++)
            {
                items.Add(testPool.AcquireItem().Result);
            }
            ConsoleWriter.Instance.WriteInformation($"Count: {testPool.Count}\tSize: {testPool.Size}");

            ConsoleWriter.Instance.WriteEmphasis("Press any key to acquire one more", true);

            var itemAdded = testPool.AcquireItem().Result;

            items.Add(itemAdded);
            ConsoleWriter.Instance.WriteInformation($"Count: {testPool.Count}\tSize: {testPool.Size}");

            ConsoleWriter.Instance.WriteEmphasis("Press any key to deactivate last added", true);
            itemAdded.Deactivate();

            ConsoleWriter.Instance.WriteInformation($"Count: {testPool.Count}\tSize: {testPool.Size}");

            ConsoleWriter.Instance.WriteEmphasis("Press any key to release all", true);

            for (var i = 0; i < items.Count; i++)
            {
                testPool.ReleaseItem(items[i]).Wait();
            }

            testPool.ContractItemPool();
            Thread.Sleep(10);

            ConsoleWriter.Instance.WriteInformation($"Count: {testPool.Count}\tSize: {testPool.Size}");

            ConsoleWriter.Instance.WriteEmphasis("Press any key to quit", true);

            testPool.Dispose();
            ConsoleWriter.Instance.Dispose();
        }