线程安全的ArrayList和HashTable

在.net 2.0中,ArrayList和HashTable都有一个封装了同步功能的新实现.

它们可以通过ArrayList.Synchronized方法或者HashTable.Synchronized方法来获得.

下面是方法的具体实现:

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
[HostProtection(SecurityAction.LinkDemand, Synchronization = true )]
public static ArrayList Synchronized(ArrayList list)
{
if (list == null )
{
throw new ArgumentNullException( " list " );
}
return new SyncArrayList(list);
}

[HostProtection(SecurityAction.LinkDemand, Synchronization
= true )]
public static IList Synchronized(IList list)
{
if (list == null )
{
throw new ArgumentNullException( " list " );
}
return new SyncIList(list);
}


[HostProtection(SecurityAction.LinkDemand, Synchronization
= true )]
public static Hashtable Synchronized(Hashtable table)
{
if (table == null )
{
throw new ArgumentNullException( " table " );
}
return new SyncHashtable(table);
}


里面的SyncHashtable之流便是同步的HashTable了,

再看看它的实现

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
[Serializable]
private class SyncHashtable : Hashtable
{
// Fields
protected Hashtable _table;

// Methods
internal SyncHashtable(Hashtable table) : base ( false )
{
this ._table = table;
}

internal SyncHashtable(SerializationInfo info, StreamingContext context) : base (info, context)
{
this ._table = (Hashtable) info.GetValue( " ParentTable " , typeof (Hashtable));
if ( this ._table == null )
{
throw new SerializationException(Environment.GetResourceString( " Serialization_InsufficientState " ));
}
}

public override void Add( object key, object value)
{
lock ( this ._table.SyncRoot)
{
this ._table.Add(key, value);
}
}

public override void Clear()
{
lock ( this ._table.SyncRoot)
{
this ._table.Clear();
}
}

public override object Clone()
{
lock ( this ._table.SyncRoot)
{
return Hashtable.Synchronized((Hashtable) this ._table.Clone());
}
}

public override bool Contains( object key)
{
return this ._table.Contains(key);
}

public override bool ContainsKey( object key)
{
return this ._table.ContainsKey(key);
}

public override bool ContainsValue( object key)
{
lock ( this ._table.SyncRoot)
{
return this ._table.ContainsValue(key);
}
}

public override void CopyTo(Array array, int arrayIndex)
{
lock ( this ._table.SyncRoot)
{
this ._table.CopyTo(array, arrayIndex);
}
}

public override IDictionaryEnumerator GetEnumerator()
{
return this ._table.GetEnumerator();
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null )
{
throw new ArgumentNullException( " info " );
}
info.AddValue(
" ParentTable " , this ._table, typeof (Hashtable));
}

public override void OnDeserialization( object sender)
{
}

public override void Remove( object key)
{
lock ( this ._table.SyncRoot)
{
this ._table.Remove(key);
}
}

internal override KeyValuePairs[] ToKeyValuePairsArray()
{
return this ._table.ToKeyValuePairsArray();
}

// Properties
public override int Count
{
get
{
return this ._table.Count;
}
}

public override bool IsFixedSize
{
get
{
return this ._table.IsFixedSize;
}
}

public override bool IsReadOnly
{
get
{
return this ._table.IsReadOnly;
}
}

public override bool IsSynchronized
{
get
{
return true ;
}
}

public override object this [ object key]
{
get
{
return this ._table[key];
}
set
{
lock ( this ._table.SyncRoot)
{
this ._table[key] = value;
}
}
}

public override ICollection Keys
{
get
{
lock ( this ._table.SyncRoot)
{
return this ._table.Keys;
}
}
}

public override object SyncRoot
{
get
{
return this ._table.SyncRoot;
}
}

public override ICollection Values
{
get
{
lock ( this ._table.SyncRoot)
{
return this ._table.Values;
}
}
}
}

这下明白了吧,就是给ArrayList和HashTable的访问方法加了个lock,

不过,这可比我们自己手写还是要靠谱的多..(注意:在进行轶代时,并不是线程安全的。如果要求绝对的线程安全,可以自行lock对象的SyncRoot)

不过有些场景可是不能lock太过的,比如读写比很高的场景(很少写,而读很多)

不过不用担心,并发环境下的缓存容器性能优化(上):不可变的哈希表

老赵的这篇文章已经有了很好的解决办法了.

 

 

 

转载于:https://www.cnblogs.com/lsjwzh/archive/2009/12/13/1623016.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值