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‘。比方说,如果我没有在这3个'GetAndMapProduct‘调用中的任何一个中对ManualResetEvent对象设置'Set()/true’,那么这3个线程会发生什么。我的意思是,如果我循环上面的代码7次,每次获取3个线程,并且没有设置ManualResetEvent,那么我根本不能使用CustomThreadPool类吗?因为CustomThreadPool假设所有(7*3)个线程都在忙于它们的工作,并且它没有任何空闲的线程可以分配?
发布于 2013-02-15 00:31:48
您没有提供任何代码来表明ManualResetEvent会以任何方式影响这3个线程。它们唯一会影响的(基于您的代码)将是通过调用WaitAll的当前线程,而根据您提供和描述的内容,这将是一个死锁。
如果您只在同一进程中管理不同的线程,我还建议您使用ManualResetEventSlim (http://msdn.microsoft.com/en-us/library/system.threading.manualreseteventslim.aspx),特别是如果您在GetAndMapProduct中所做的事情相当快的话。但是,这真的不清楚你想要实现什么。
https://stackoverflow.com/questions/14875036
复制相似问题