构造用于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"); }