zoukankan      html  css  js  c++  java
  • C# SyncDictionary类

    代码
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Threading;

    namespace Rocky
    {
    public class SyncDictionary : IDictionary
    {
    #region wrap
    private IDictionary wrapDictionary;

    public SyncDictionary()
    :
    this(new HybridDictionary())
    {

    }
    public SyncDictionary(IDictionary dictionary)
    {
    if (dictionary == null)
    {
    throw new ArgumentNullException("dictionary");
    }
    wrapDictionary
    = dictionary;
    }
    #endregion

    #region IDictionary 成员
    public void Add(object key, object value)
    {
    if (!wrapDictionary.Contains(key))
    {
    lock (wrapDictionary.SyncRoot)
    {
    if (!wrapDictionary.Contains(key))
    {
    wrapDictionary.Add(key, value);
    }
    }
    }
    }

    public void Clear()
    {
    if (wrapDictionary.Count > 0)
    {
    lock (wrapDictionary.SyncRoot)
    {
    if (wrapDictionary.Count > 0)
    {
    wrapDictionary.Clear();
    }
    }
    }
    }

    public bool Contains(object key)
    {
    return wrapDictionary.Contains(key);
    }

    public IDictionaryEnumerator GetEnumerator()
    {
    return wrapDictionary.GetEnumerator();
    }

    bool IDictionary.IsFixedSize
    {
    get { return wrapDictionary.IsFixedSize; }
    }

    bool IDictionary.IsReadOnly
    {
    get { return wrapDictionary.IsReadOnly; }
    }

    public ICollection Keys
    {
    get
    {
    lock (wrapDictionary.SyncRoot)
    {
    return wrapDictionary.Keys;
    }
    }
    }

    public void Remove(object key)
    {
    if (wrapDictionary.Contains(key))
    {
    lock (wrapDictionary.SyncRoot)
    {
    if (wrapDictionary.Contains(key))
    {
    wrapDictionary.Remove(key);
    }
    }
    }
    }

    public ICollection Values
    {
    get
    {
    lock (wrapDictionary.SyncRoot)
    {
    return wrapDictionary.Values;
    }
    }
    }

    public object this[object key]
    {
    set
    {
    lock (wrapDictionary.SyncRoot)
    {
    wrapDictionary[key]
    = value;
    }
    }
    get
    {
    return wrapDictionary[key];
    }
    }
    #endregion

    #region ICollection 成员
    public void CopyTo(Array array, int index)
    {
    lock (wrapDictionary.SyncRoot)
    {
    wrapDictionary.CopyTo(array, index);
    }
    }

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

    bool ICollection.IsSynchronized
    {
    get { return true; }
    }

    object ICollection.SyncRoot
    {
    get { return wrapDictionary.SyncRoot; }
    }
    #endregion

    #region IEnumerable 成员
    IEnumerator IEnumerable.GetEnumerator()
    {
    return this.GetEnumerator();
    }
    #endregion
    }

    public class SyncDictionary<TKey, TValue> : IDictionary<TKey, TValue>
    {
    #region wrap
    private object syncRoot;
    private IDictionary<TKey, TValue> wrapDictionary;

    private object SyncRoot
    {
    get
    {
    if (syncRoot == null)
    {
    Interlocked.CompareExchange(
    ref syncRoot, new object(), null);
    }
    return syncRoot;
    }
    }

    public SyncDictionary()
    :
    this(new Dictionary<TKey, TValue>())
    {

    }
    public SyncDictionary(IDictionary<TKey, TValue> dictionary)
    {
    if (dictionary == null)
    {
    throw new ArgumentNullException("dictionary");
    }
    wrapDictionary
    = dictionary;
    }
    #endregion

    #region IDictionary<TKey,TValue> 成员
    public void Add(TKey key, TValue value)
    {
    if (!wrapDictionary.ContainsKey(key))
    {
    lock (this.SyncRoot)
    {
    if (!wrapDictionary.ContainsKey(key))
    {
    wrapDictionary.Add(key, value);
    }
    }
    }
    }

    public bool ContainsKey(TKey key)
    {
    return wrapDictionary.ContainsKey(key);
    }

    public ICollection<TKey> Keys
    {
    get
    {
    lock (this.SyncRoot)
    {
    return wrapDictionary.Keys;
    }
    }
    }

    public bool Remove(TKey key)
    {
    if (wrapDictionary.ContainsKey(key))
    {
    lock (this.SyncRoot)
    {
    if (wrapDictionary.ContainsKey(key))
    {
    return wrapDictionary.Remove(key);
    }
    }
    }
    return false;
    }

    public bool TryGetValue(TKey key, out TValue value)
    {
    lock (this.SyncRoot)
    {
    return wrapDictionary.TryGetValue(key, out value);
    }
    }

    public ICollection<TValue> Values
    {
    get
    {
    lock (this.SyncRoot)
    {
    return wrapDictionary.Values;
    }
    }
    }

    public TValue this[TKey key]
    {
    set
    {
    lock (this.SyncRoot)
    {
    wrapDictionary[key]
    = value;
    }
    }
    get
    {
    TValue value;
    wrapDictionary.TryGetValue(key,
    out value);
    return value;
    }
    }
    #endregion

    #region ICollection<KeyValuePair<TKey,TValue>> 成员
    void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
    {
    lock (this.SyncRoot)
    {
    ((ICollection
    <KeyValuePair<TKey, TValue>>)wrapDictionary).Add(item);
    }
    }

    public void Clear()
    {
    if (wrapDictionary.Count > 0)
    {
    lock (this.SyncRoot)
    {
    if (wrapDictionary.Count > 0)
    {
    wrapDictionary.Clear();
    }
    }
    }
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
    {
    return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).Contains(item);
    }

    public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
    {
    lock (this.SyncRoot)
    {
    ((ICollection
    <KeyValuePair<TKey, TValue>>)wrapDictionary).CopyTo(array, arrayIndex);
    }
    }

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

    bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
    {
    get { return false; }
    }

    bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
    {
    lock (this.SyncRoot)
    {
    return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).Remove(item);
    }
    }
    #endregion

    #region IEnumerable<KeyValuePair<TKey,TValue>> 成员
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
    {
    return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).GetEnumerator();
    }
    #endregion

    #region IEnumerable 成员
    IEnumerator IEnumerable.GetEnumerator()
    {
    return this.GetEnumerator();
    }
    #endregion
    }
    }
  • 相关阅读:
    Scanner扫描器
    数据类型的转换
    万年历java
    冒泡排序、选择排序(升序排序,降序排序)
    数组
    字符串大小写转换(全转)(大小写互转)
    数据库两表联查、多表联查,多重联查
    js监测输入框字数(一个汉字等于两个字母),超出限制输入无效
    几条经验让jQuery用的更漂亮
    JS实现URL的拼接转换
  • 原文地址:https://www.cnblogs.com/Googler/p/1756447.html
Copyright © 2011-2022 走看看