自定义Dictionary支持线程安全
生活随笔
收集整理的這篇文章主要介紹了
自定义Dictionary支持线程安全
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文轉載:http://www.cnblogs.com/kiddo/archive/2008/09/25/1299089.html
我們說一個數據結構是線程安全指的是同一時間只有一個線程可以改寫它。這樣即使多個線程來訪問它,它也不會產生對線程來說很意外的數據。 C#中的Dictionary不是線程安全的,我在下面這個例子中,把一個Dictionary對象作為了全局的static變量。會有多個線程來訪問它。所以我需要包裝一下.net自帶的Dictionrary. 發生沖突的部分無非是寫的地方,所以在離寫Dictionary最近的地方加一個鎖。其他的外層代碼可以自帶的Dictionary相同了。 我們看Dictionary的實現接口, 自定義一個線程安全的數據對象類。 public class SafeDictionary<TKey, TValue> : IDictionary<TKey, TValue>{private readonly object syncRoot = new object();private readonly Dictionary<TKey, TValue> d = new Dictionary<TKey, TValue>();#region IDictionary<TKey,TValue> Members/// <summary>/// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <param name="key">The object to use as the key of the element to add.</param>/// <param name="value">The object to use as the value of the element to add.</param>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>/// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</exception>/// <exception cref="T:System.ArgumentNullException">key is null.</exception>public void Add(TKey key, TValue value){lock (syncRoot){d.Add(key, value);}}/// <summary>/// Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the specified key./// </summary>/// <param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</param>/// <returns>/// true if the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the key; otherwise, false./// </returns>/// <exception cref="T:System.ArgumentNullException">key is null.</exception>public bool ContainsKey(TKey key){return d.ContainsKey(key);}/// <summary>/// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <value></value>/// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>public ICollection<TKey> Keys{get{lock (syncRoot){return d.Keys;}}}/// <summary>/// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <param name="key">The key of the element to remove.</param>/// <returns>/// true if the element is successfully removed; otherwise, false. This method also returns false if key was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </returns>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>/// <exception cref="T:System.ArgumentNullException">key is null.</exception>public bool Remove(TKey key){lock (syncRoot){return d.Remove(key);}}/// <summary>/// Tries the get value./// </summary>/// <param name="key">The key.</param>/// <param name="value">The value.</param>/// <returns></returns>public bool TryGetValue(TKey key, out TValue value){lock (syncRoot){return d.TryGetValue(key, out value);}}/// <summary>/// Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>./// </summary>/// <value></value>/// <returns>An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</returns>public ICollection<TValue> Values{get{lock (syncRoot){return d.Values;}}}/// <summary>/// Gets or sets the <see cref="TValue"/> with the specified key./// </summary>/// <value></value>public TValue this[TKey key]{get { return d[key]; }set{lock (syncRoot){d[key] = value;}}}/// <summary>/// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>public void Add(KeyValuePair<TKey, TValue> item){lock (syncRoot){((ICollection<KeyValuePair<TKey, TValue>>)d).Add(item);}}/// <summary>/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>public void Clear(){lock (syncRoot){d.Clear();}}/// <summary>/// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value./// </summary>/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>/// <returns>/// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false./// </returns>public bool Contains(KeyValuePair<TKey, TValue> item){return ((ICollection<KeyValuePair<TKey, TValue>>)d).Contains(item);}/// <summary>/// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index./// </summary>/// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>/// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>/// <exception cref="T:System.ArgumentNullException">array is null.</exception>/// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex){lock (syncRoot){((ICollection<KeyValuePair<TKey, TValue>>)d).CopyTo(array, arrayIndex);}}/// <summary>/// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <value></value>/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>public int Count{get { return d.Count; }}/// <summary>/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only./// </summary>/// <value></value>/// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>public bool IsReadOnly{get { return false; }}/// <summary>/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </summary>/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>/// <returns>/// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>./// </returns>/// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>public bool Remove(KeyValuePair<TKey, TValue> item){lock (syncRoot){return ((ICollection<KeyValuePair<TKey, TValue>>)d).Remove(item);}}/// <summary>/// Returns an enumerator that iterates through the collection./// </summary>/// <returns>/// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection./// </returns>public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(){return ((ICollection<KeyValuePair<TKey, TValue>>)d).GetEnumerator();}/// <summary>/// Returns an enumerator that iterates through a collection./// </summary>/// <returns>/// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection./// </returns>IEnumerator IEnumerable.GetEnumerator(){return ((IEnumerable)d).GetEnumerator();}#endregion}微軟4.0框架已經有了這個線程安全的Dictionary。
ConcurrentDictionary<TKey, TValue> 類
詳細可以查看:http://msdn.microsoft.com/zh-cn/library/dd287191(v=vs.110).aspx http://www.cnblogs.com/atskyline/p/3234805.html 4.0中新增的線程安全集合類:
| 線程安全集合類 | 非線程安全集合類 |
ConcurrentQueue<T> | Queue<T> |
ConcurrentStack<T> | Stack<T> |
ConcurrentBag<T> | List<T> |
?ConcurrentDictionary<TKey,TValue> | ? Dictionary<TKey,TValue> |
轉載于:https://www.cnblogs.com/51net/p/3963815.html
總結
以上是生活随笔為你收集整理的自定义Dictionary支持线程安全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Winform 导出成Excel打印代码
- 下一篇: Android项目的目录结构