是ConcurrentDictionary和ConcurrentQueue的结合线程安全的?线程、安全、ConcurrentDictionary、ConcurrentQueue

由网友(暴富)分享简介:我利用.NET 4的ConcurrentDictionary和ConcurrentQueue类以下code。这是code线程安全的?如果没有,我怎么可以把它线程安全的?公共类页{公共字符串名称{;组; }}公共类PageQueue{私人ConcurrentDictionary< INT,Concurrent...

我利用.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;
    }
}
 
C 中HashTable Dictionary ConcurrentDictionary区别

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;
    }
}

阅读全文

相关推荐

最新文章