热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#SyncDictionary类

代码usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Collecti

 

ContractedBlock.gifExpandedBlockStart.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 &#61;&#61; 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 &#61;&#61; null)
{
throw new ArgumentNullException("dictionary");
}
wrapDictionary
&#61; dictionary;
}
#endregion

#region IDictionary 成员
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]
&#61; value;
}
}
get
{
TValue value;
wrapDictionary.TryGetValue(key,
out value);
return value;
}
}
#endregion

#region ICollection> 成员
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> 成员
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).GetEnumerator();
}
#endregion

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

 

 

 


转载于:https://www.cnblogs.com/Googler/archive/2010/06/11/1756447.html


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
author-avatar
我以为我疯叻_219
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有