C# SyncDictionary类

本文介绍了一个 C# 中的 SyncDictionary 类实现,该类通过锁机制实现了对字典操作的线程同步,确保了多线程环境下数据的一致性和安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C# SyncDictionary类

 

ContractedBlock.gif ExpandedBlockStart.gif 代码

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

 

 

 

posted on 2010-06-11 16:01 RockyLOMO 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值