构造用于1个方法在多线程环境下重复多次执行测试的通用方法 C#

        static void MultithreadsCycleTestWithSameMethod(int threadNumber, int cycleNumber, ThreadStart action)
        {
            Console.WriteLine("Begin");
            var threads = new Thread[threadNumber];
            ThreadStart threadStart = () =>
            {
                for (int i = 0; i < cycleNumber; i++)
                {
                    action();
                }
            };
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(threadStart);
            }
            foreach (var thread in threads)
            {
                thread.Start();
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }
            Console.WriteLine("End");
        }

 

posted on 2017-12-31 14:57  方辰  阅读(349)  评论(0编辑  收藏  举报

导航