是ConcurrentDictionary和ConcurrentQueue的结合线程安全的? [英] Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?
本文介绍了是ConcurrentDictionary和ConcurrentQueue的结合线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我利用.NET 4的ConcurrentDictionary和ConcurrentQueue类以下code。
这是code线程安全的?如果没有,我怎么可以把它线程安全的?
公共类页
{
公共字符串名称{;组; }
}
公共类PageQueue
{
私人ConcurrentDictionary< INT,ConcurrentQueue<第>>网页=
新ConcurrentDictionary< INT,ConcurrentQueue<第>>();
公共无效添加(INT ID,页页)
{
如果(!this.pages.ContainsKey(ID))
this.pages [ID] =新ConcurrentQueue<第>();
this.pages [ID] .Enqueue(页);
}
公共页面GetAndRemove(INT ID)
{
页面LP = NULL;
如果(this.pages.ContainsKey(ID))
this.pages [ID] .TryDequeue(出罐);
返回LP;
}
}
演示:
公共类演示
{
公共无效RunAll()
{
的for(int i = 0;我小于10;我++)
Task.Factory.StartNew(()=>运行());
}
公共无效的run()
{
PageQueue PQ =新PageQueue();
pq.Add(1,新的页面());
pq.GetAndRemove(1);
}
}
解决方案
由于@Femaref正确地指出的那样,在你的code一些缺陷。我建议你通过 ConcurrentDictionary&LT提供的众多方法的优势; K,V> ,以使您的code线程安全的,无需锁定
语句:
公共类PageQueue
{
私人ConcurrentDictionary< INT,ConcurrentQueue<第>>网页=
新ConcurrentDictionary< INT,ConcurrentQueue<第>>();
公共无效排队(INT ID,页页)
{
VAR队列= this.pages.GetOrAdd(ID,_ =>新建ConcurrentQueue<第>());
queue.Enqueue(页);
}
公共BOOL TryDequeue(INT ID,出页)
{
ConcurrentQueue<第>队列;
如果(this.pages.TryGetValue(ID,出队列))
{
返回queue.TryDequeue(下页);
}
页=无效;
返回false;
}
}
I'm using the ConcurrentDictionary and ConcurrentQueue classes from .NET 4 in the following code.
Is this code thread-safe? If not, how can I make it thread-safe?
public class Page
{
public string Name {get; set; }
}
public class PageQueue
{
private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages =
new ConcurrentDictionary<int, ConcurrentQueue<Page>>();
public void Add(int id, Page page)
{
if (!this.pages.ContainsKey(id))
this.pages[id] = new ConcurrentQueue<Page>();
this.pages[id].Enqueue(page);
}
public Page GetAndRemove(int id)
{
Page lp = null;
if(this.pages.ContainsKey(id))
this.pages[id].TryDequeue(out lp);
return lp;
}
}
Demo:
public class Demo
{
public void RunAll()
{
for (int i = 0; i < 10; i++)
Task.Factory.StartNew(() => Run());
}
public void Run()
{
PageQueue pq = new PageQueue();
pq.Add(1, new Page());
pq.GetAndRemove(1);
}
}
解决方案
As @Femaref correctly pointed out, there are some flaws in your code. I suggest you take advantage of the many methods offered by ConcurrentDictionary<K,V> to make your code thread-safe without the need for lock
statements:
public class PageQueue
{
private ConcurrentDictionary<int, ConcurrentQueue<Page>> pages =
new ConcurrentDictionary<int, ConcurrentQueue<Page>>();
public void Enqueue(int id, Page page)
{
var queue = this.pages.GetOrAdd(id, _ => new ConcurrentQueue<Page>());
queue.Enqueue(page);
}
public bool TryDequeue(int id, out Page page)
{
ConcurrentQueue<Page> queue;
if (this.pages.TryGetValue(id, out queue))
{
return queue.TryDequeue(out page);
}
page = null;
return false;
}
}
这篇关于是ConcurrentDictionary和ConcurrentQueue的结合线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文