List<ManualResetEvent> doneEventsList = new List<ManualResetEvent>();
productBuilders.ForEach(productBuilder =>
{
doneEventsList.Add(productBuilder.GetDoneEvent());
CustomThreadPool.getSingletonInstance.QueueUserWorkItem(productBuilder.GetAndMapProduct, new Object());
});
WaitHandle.WaitAll(doneEventsList.ToArray());
我对上面的代码有一点怀疑,
假设CustomThreadPool
有 21 个Thread
“类”实例的列表,而“ productBuilders
”有 3 个构建器。在这种情况下,从 CustomThreadPool 获取的线程计数将为“3”。可以说,如果我在ManualResetEvent
这 3 个“”调用中的任何一个中都没有在对象上设置“Set()/true” GetAndMapProduct
,那么这 3 个线程会发生什么。我的意思是,如果我在代码上方循环 7 次并且每次获取 3 个线程而不设置ManualResetEvent
,我就不能使用 CustomThreadPool 类吗?因为 CustomThreadPool 假设所有(7 * 3)线程都忙于工作并且它没有有任何空闲线程可以分配吗?