title: Is this combination of ConcurrentDictionary and ConcurrentQueue thread-safe?
body: <p>I'm using the ConcurrentDictionary and ConcurrentQueue classes from .NET 4 in the following code.</p>
  
  <p>Is this code thread-safe? If not, how can I make it thread-safe?</p>
  
  <pre><code>public class Page
  {
      public string Name {get; set; }
  }
  
  public class PageQueue
  {
      private ConcurrentDictionary&lt;int, ConcurrentQueue&lt;Page&gt;&gt; pages =
          new ConcurrentDictionary&lt;int, ConcurrentQueue&lt;Page&gt;&gt;();
  
      public void Add(int id, Page page)
      {
          if (!this.pages.ContainsKey(id))
              this.pages[id] = new ConcurrentQueue&lt;Page&gt;();
  
          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;
      }
  }
  </code></pre>
  
  <p>Demo:</p>
  
  <pre><code> public class Demo
   {
       public void RunAll()
       {
           for (int i = 0; i &lt; 10; i++)
               Task.Factory.StartNew(() =&gt; Run());
       }
  
       public void Run()
       {
           PageQueue pq = new PageQueue();
           pq.Add(1, new Page());
  
           pq.GetAndRemove(1);
       }
   }
  </code></pre>