Ejemplo n.º 1
0
        public void PoolShouldProceedSeveralNestedTasksFromOneTaskCorrectly()
        {
            var pool     = new CustomThreading.MyThreadPool(5);
            var mainTask = pool.AddTask(() => 5);

            var nestedTasks = new CustomThreading.IMyTask <int> [10];

            for (int i = 0; i < 10; ++i)
            {
                var localI = i;
                nestedTasks[i] = mainTask.ContinueWith(mainValue => mainValue + localI);
            }

            for (int i = 0; i < 10; ++i)
            {
                Assert.AreEqual(5 + i, nestedTasks[i].Result);
            }
        }
Ejemplo n.º 2
0
        public void ShouldWorkCorrectlyWhenTaskCountMoreThanThreadCount()
        {
            var pool  = new CustomThreading.MyThreadPool(3);
            var tasks = new CustomThreading.IMyTask <int> [10];

            for (int i = 0; i < 10; ++i)
            {
                var localI = i;
                tasks[i] = pool.AddTask(() =>
                {
                    System.Threading.Thread.Sleep(100);
                    return(localI);
                });
            }

            for (int i = 0; i < 10; ++i)
            {
                Assert.AreEqual(i, tasks[i].Result);
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            const int taskCount = 10;
            const int availableThreadsInPool = 3;

            var pool = new CustomThreading.MyThreadPool(availableThreadsInPool);

            var tasks = new CustomThreading.IMyTask <int> [taskCount];

            for (int i = 0; i < tasks.Length; ++i)
            {
                tasks[i] = pool.AddTask(TaskReturn15);
            }

            Console.WriteLine("There should be 15 (x{0}):", taskCount);
            foreach (var task in tasks)
            {
                Console.WriteLine(task.Result);
            }

            var nestedTasks = new CustomThreading.IMyTask <int> [taskCount];

            for (int i = 0; i < tasks.Length; ++i)
            {
                nestedTasks[i] = tasks[i].ContinueWith(TaskReturnOldPlus38);
            }

            Console.WriteLine("There should be 53 (x{0}):", taskCount);
            foreach (var task in nestedTasks)
            {
                Console.WriteLine(task.Result);
            }

            Console.WriteLine("And there should be 15 (x{0}) again:", taskCount);
            foreach (var task in tasks)
            {
                Console.WriteLine(task.Result);
            }
        }
Ejemplo n.º 4
0
        public void PoolShutdownShouldNotAffectPreviousTasks()
        {
            var pool = new CustomThreading.MyThreadPool(10);

            var tasks = new CustomThreading.IMyTask <int> [10];

            for (int i = 0; i < 10; ++i)
            {
                var localI = i;
                tasks[i] = pool.AddTask(() =>
                {
                    System.Threading.Thread.Sleep(50);
                    return(localI);
                });
            }
            System.Threading.Thread.Sleep(50);

            pool.Shutdown();

            for (int i = 0; i < 10; ++i)
            {
                Assert.AreEqual(i, tasks[i].Result);
            }
        }