コード例 #1
0
        public void TestChooseThread()
        {
            var pool        = new ThreadPool();
            var threadCount = pool.ThreadCountMax;

            Assert.AreEqual(threadCount, ThreadPool.DefaultMaxThreadCount);
            Assert.AreEqual(pool.ThreadCount, 0);
            HashSet <int> idSet = new HashSet <int>();

            for (int i = 0; i < threadCount; i++)
            {
                var thread = pool.ChooseThread();
                idSet.Add(thread.Id);
            }
            Assert.AreEqual(idSet.Count, threadCount);
            Assert.AreEqual(pool.ThreadCount, threadCount);

            idSet.Clear();
            for (int i = 0; i < threadCount; i++)
            {
                var thread = pool.ChooseThreadFast();
                idSet.Add(thread.Id);
            }
            Assert.AreEqual(idSet.Count, threadCount);

            pool.Stop();
        }