我有一个场景,其中多个生产者在网络中的不同机器上运行。这些是Singleton WCF服务,它将产品(输出)排队在Central Store中,这也是Singleton WCF服务。多个生产者,多个消费者和商店问题
有些消费者通过JSON请求调用Central Store来从Central Store出厂产品。产品通过解决某些优先级限制来交付。生产者在1万分钟内以非常高的速度生产产品,目的是以同样的速度向消费者提供服务,而不是让他们等待。
只要我有3-4个生产者和多达10个消费者,一切正常。但是,当我增加生产者和消费者的一切冻结。
我使用的是TimedLock这是Monitor.TryEnter的包装。我尝试了所有类型的同步技术,如ReaderWriterLockSlim和Web上的其他帖子,但结果相同。我避免了ReaderWriterLockSlim,因为我有大量的写入而不是读取。
由于我无法控制消费者和生产者线程,这些线程由WCF产生,因为他们访问的是Singleton Store。我无法实施网上提供的产品/消费者样品。以下是数据存储的示例代码。
public class Store:IEnumerable<Product>
{
private List<Product> _Products;
private object _MonitorLock;
public Store()
{
_Products = new List<Product>();
_MonitorLock = new object();
}
public void Add(Product Product)
{
lock (_MonitorLock)
{
_Products.Add(Product);
}
}
public void Remove(Product Product)
{
lock (_MonitorLock)
{
_Products.Remove(Product);
}
}
public int Count
{
get
{
lock (_MonitorLock)
{
return _Products.Count;
}
}
}
public IEnumerator<Product> GetEnumerator()
{
List<Product> ProductsCopy;
lock (_MonitorLock)
{
ProductsCopy = new List<Product>(_Products);
}
foreach (Product oEntry in ProductsCopy)
yield return oEntry;
}
public Product GetHighestPriorityProduct()
{
Product oProduct;
lock (_MonitorLock)
{
//Some Logic to Resolve the Priority
_Products.Remove(oProduct);
}
return oProduct;
}
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
在深入研究问题后,我发现应用程序冻结时仅增加了消费者数量。如果我增加生产者但保持消费者低,它不会冻结。 正如您已经指出的那样,冻结可能是因为优先解析需要长时间,并且lock()在整个时间内都会被获取。 我会尝试优先队列并发布反馈。感谢您的建议。 – Protean 2009-09-20 10:50:33