我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们之间有联系吗?我怎么才能用好他们呢?
我们以Hashtable为例,看看他们的基本用法:
Hashtable ht = Hashtable.Synchronized(new Hashtable());
lock (ht.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//一些操作
}
1,Synchronized表示返回一个线程安全的Hashtable,什么样的hashtable才是一个线程安全的呢?下边我们就从.NET的源码开始理解。
public static Hashtable Synchronized(Hashtable table)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (table == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("table");
}
return new SyncHashtable(table);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从源码不难看出,Synchronized方法返回的其实是一个SynchHashtable类型的实例。在前边我们说过,
Synchronized表示返回一个线程安全的Hashtable,从这个解释不难看出,SynchHashtable应该是继承自Hashtable。下边我们验证一下。看看SynchHashtable类型的源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
[Serializable]
private class SyncHashtable : Hashtable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
// Fields
protected Hashtable _table;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Methods
internal SyncHashtable(Hashtable table) : base(false)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table = table;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal SyncHashtable(SerializationInfo info, StreamingContext context) : base(info, context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table = (Hashtable) info.GetValue("ParentTable", typeof(Hashtable));
if (this._table == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Add(object key, object value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table.Add(key, value);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Clear()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table.Clear();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override object Clone()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Hashtable.Synchronized((Hashtable) this._table.Clone());
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool Contains(object key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.Contains(key);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool ContainsKey(object key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.ContainsKey(key);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool ContainsValue(object key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.ContainsValue(key);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void CopyTo(Array array, int arrayIndex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table.CopyTo(array, arrayIndex);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override IDictionaryEnumerator GetEnumerator()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.GetEnumerator();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void GetObjectData(SerializationInfo info, StreamingContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (info == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new ArgumentNullException("info");
}
info.AddValue("ParentTable", this._table, typeof(Hashtable));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void OnDeserialization(object sender)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override void Remove(object key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table.Remove(key);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal override KeyValuePairs[] ToKeyValuePairsArray()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.ToKeyValuePairsArray();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Properties
public override int Count
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.Count;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool IsFixedSize
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.IsFixedSize;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool IsReadOnly
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.IsReadOnly;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override bool IsSynchronized
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override object this[object key]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table[key];
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._table[key] = value;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override ICollection Keys
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.Keys;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override object SyncRoot
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.SyncRoot;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override ICollection Values
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
lock (this._table.SyncRoot)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._table.Values;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Collapse Methods
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
呵呵,果然不出我们所料,
SyncHashtable果然继承自Hashtable,SyncHashtable之所有能实现线程的安全操作,就是因为在他们的一些方法中,就加了lock,我们知道,哪一个线程执行了lock操作,在他还没有释放lock之前,其他线程都要处于堵塞状态。SyncHashtable就是通过这种方法,来实现所谓的线程安全。
现在我们理解了
Synchronized的含义和用法,那接下来我们看看他和
SyncRoot之间的关系。
SyncRoot表示
获取可用于同步 Hashtable 访问的对象,老实说,这个解释不好理解,要想真正理解他的用法,我们还得从源码开始:
public virtual object SyncRoot
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this._syncRoot == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
}
return this._syncRoot;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果您清楚Interlocked的用法,这段代码没什么难理解的了(不清楚的朋友找GOOGLE吧),Interlocked为多个线程共享的变量提供原子操作。 原子操作就是单线程操作。在一个Hashtable实例中,不论我们在代码的任何位置调用,返回的都是同一个object类型的对象。我们在开始写的lock(ht.SyncRoot)和下边的操作作用是一样的.
static object obj = new object();
lock(obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//一些操作
}
他们之间不同的是,我们声明的static object类型对象是类型级别的,而SyncRoot是对象级别的。
通过上面的分析,我们都应该能理解Synchronized 和 SyncRoot用法,他们之间的关系就是:
Hashtable通过Synchronized方法,生成一个SynchHashtable类型的对象,在这个对象的一个方法中,通过lock (this._table.SyncRoot)这样的代码来实现线程安全的操作,其中this._table.SyncRoot返回的就是一个object类型的对象,在一个SynchHashtable对象实例中,不管我们调用多少次,他是唯一的。