If I were in your place, I would use ThreadPool instead of manual threads. It handles all these things itself, and you do not have the overhead of creating and destroying threads. The code will probably be a bit more complicated because you will need to use ManualResetEvent instead of just Thread.Join () (see How can I execute ThreadPool.Join? ), But you don’t have to worry about creating too many threads, and that’s almost 40 times faster.
In this class for the test, I wrote a comparison of two approaches:
class ThreadPoolVsThreads { private static readonly PerformanceMonitor threadPoolTest = new PerformanceMonitor("ThreadPoolTest"); private static readonly PerformanceMonitor threadTest = new PerformanceMonitor("ThreadTest"); private const int iterations = 100; private const int threads = 10; private static long somevalue; public static void Test() { TestHelper.PerformTest(10, threadPoolTest, ThreadPoolTest); TestHelper.PerformTest(10, threadTest, ThreadTest); } private static void ThreadPoolTest(int iteration) { for (int i = 0; i < iterations; i++) { var resetEvents = new ManualResetEvent[threads]; for (int j = 0; j < threads; j++) { var re = new ManualResetEvent(false); resetEvents[j] = re; ThreadPool.QueueUserWorkItem(o => { somevalue++; re.Set(); }); } WaitHandle.WaitAll(resetEvents); } } private static void ThreadTest(int iteration) { for (int i = 0; i < iterations; i++) { var threadArray = new Thread[threads]; for (int j = 0; j < threads; j++) { var thread = new Thread(o => somevalue++); threadArray[j] = thread; thread.Start(); } for (int j = 0; j < threads; j++) { threadArray[j].Join(); } } } }
And here is the output for five runs:
ThreadPoolTest action completed: iteration = 1, completeTime = 53, averageCompletionTime = 53.000
ThreadTest action completed: iteration = 1, completeTime = 2128, averageCompletionTime = 2128.000
ThreadPoolTest action completed: iteration = 2, completeTime = 42, averageCompletionTime = 47.500
ThreadTest action completed: iteration = 2, completeTime = 2149, averageCompletionTime = 2138.500
ThreadPoolTest action completed: iteration = 3, completeTime = 65, averageCompletionTime = 53.333
ThreadTest action completed: iteration = 3, completeTime = 2078, averageCompletionTime = 2118.333
ThreadPoolTest action completed: iteration = 4, completeTime = 72, averageCompletionTime = 58.000
ThreadTest action completed: iteration = 4, completeTime = 2137, averageCompletionTime = 2123.000
ThreadPoolTest action completed: iteration = 5, completeTime = 43, averageCompletionTime = 55.000
ThreadTest action completed: iteration = 5, completeTime = 2085, averageCompletionTime = 2115.400