2011-05-01 95 views
2

我在这里要求任何ConcurentSet/ConcurentQueue执行how to create no-duplicates ConcurrentQueue?,但没有人提出任何建议。为什么我的ConcurentSet不起作用?

所以我决定自己写了,稍微修改了MSDN例子。 我创建了ConcurentSet类。我插入相同的元素两次,并期望它只能插入一次,因为Set用于内部:

   numbers.Add(i); 
       Console.WriteLine("Add:{0} Count={1}", i, numbers.Count); 
       numbers.Add(i); 
       Console.WriteLine("Add:{0} Count={1}", i, numbers.Count); 

然而,根据两次插入输出要素:

Add:0 Count=0 
    Add:0 Count=1 
    Add:1 Count=2 
    Add:1 Count=3 
    Add:2 Count=4 
    Take:0 
    Add:2 Count=5 
    Add:3 Count=6 
    Add:3 Count=7 
    Add:4 Count=7 
    Add:4 Count=8 
    Take:3 
    Add:5 Count=9 
    Add:5 Count=10 
    Add:6 Count=11 
    Add:6 Count=12 

的问题是 - 为什么我的ConcurentSet实现不按预期工作? 我希望这样的输出:

Add:0 Count=0 
    Add:0 Count=0 
    Add:1 Count=1 
    Add:1 Count=1 
    Add:2 Count=2 
    Take:0 
    Add:2 Count=1 
    Add:3 Count=2 
    Add:3 Count=2 
    Add:4 Count=3 
    Add:4 Count=3 
    Take:3 
    Add:5 Count=3 
    Add:5 Count=3 
    ..... 

完整清单如下:

using System.Collections; 
using System.Collections.Generic; 
using System.Threading; 
using System.Threading.Tasks; 

namespace BCBlockingAccess 
{ 
using System; 
using System.Collections.Concurrent; 

public class ConcurentSet 
: IProducerConsumerCollection<int> 
{ 
    private readonly object m_lockObject = new object(); 
    private readonly HashSet<int> m_set = new HashSet<int>(); 

    public void CopyTo(Array array, int index) 
    { 
     throw new NotImplementedException(); 
    } 

    public int Count { get { return m_set.Count; } } 

    public object SyncRoot { get { return m_lockObject; } } 

    public bool IsSynchronized { get { return true; } } 

    public void CopyTo(int[] array, int index) 
    { 
     throw new NotImplementedException(); 
    } 

    public bool TryAdd(int item) 
    { 
     lock (m_lockObject) 
     { 
      m_set.Add(item); 
     } 

     return true; 
    } 

    public bool TryTake(out int item) 
    { 
     lock (m_lockObject) 
     { 
      foreach (var i in m_set) 
      { 
       if (m_set.Remove(i)) 
       { 
        item = i; 
        return true; 
       } 
      } 
      item = -1; 
      return false; 
     } 
    } 

    public int[] ToArray() 
    { 
     throw new NotImplementedException(); 
    } 

    public IEnumerator<int> GetEnumerator() 
    { 
     throw new NotImplementedException(); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 
} 


class Program 
{ 
    static void Main(string[] args) 
    { 
     // Increase or decrease this value as desired. 
     int itemsToAdd = 50; 

     // Preserve all the display output for Adds and Takes 
     Console.SetBufferSize(80, (itemsToAdd * 5) + 3); 

     // A bounded collection. Increase, decrease, or remove the 
     // maximum capacity argument to see how it impacts behavior. 
     BlockingCollection<int> numbers = new BlockingCollection<int>(new ConcurentSet()); 


     // A simple blocking consumer with no cancellation. 
     Task.Factory.StartNew(() => 
     { 
      int i = -1; 
      while (!numbers.IsCompleted) 
      { 
       try 
       { 
        i = numbers.Take(); 
       } 
       catch (InvalidOperationException) 
       { 
        Console.WriteLine("Adding was compeleted!"); 
        break; 
       } 
       Console.WriteLine("Take:{0} ", i); 

       // Simulate a slow consumer. This will cause 
       // collection to fill up fast and thus Adds wil block. 
       Thread.SpinWait(100000); 
      } 

      Console.WriteLine("\r\nNo more items to take. Press the Enter key to exit."); 
     }); 

     // A simple blocking producer with no cancellation. 
     Task.Factory.StartNew(() => 
     { 
      for (int i = 0; i < itemsToAdd; i++) 
      { 
       numbers.Add(i); 
       Console.WriteLine("Add:{0} Count={1}", i, numbers.Count); 
       numbers.Add(i); 
       Console.WriteLine("Add:{0} Count={1}", i, numbers.Count); 
      } 

      // See documentation for this method. 
      numbers.CompleteAdding(); 
     }); 

     // Keep the console display open in debug mode. 

     Console.ReadLine(); 
    } 
} 
} 
+0

Eiter某些东西非常坏或者你的测试代码和输出不匹配:在第一个Add()之后计数== 0? – 2011-05-01 11:56:00

+0

@亨克,他使用'BlockingCollection ',他将这个集合作为构造函数的参数。所以当他调用Add()时,BlockingCollection 调用他的类的TryAdd()。 – svick 2011-05-01 14:23:55

+0

@svick,好的,谢谢,我错过了。 – 2011-05-01 15:23:23

回答

3

BlockingCollection维护自己的计数,因此即使忽略重复项,计数增加不使用你的潜在ConcurrentSet的数。

您可能希望围绕此集合编写自己的包装,该包装从concurrentset中返回计数,并将每个其他方法属性中继到阻塞集合。

+0

除此之外,我的代码一切正常?为什么BlockingCollection使用它自己的Count?这似乎完全没用。 – javapowered 2011-05-02 07:33:49

+0

是的,你的代码是好的。是的,保持自己的计数似乎是没用的,但Eric Lippert可能会对此有所考虑。 – 2011-05-02 08:52:13