From: http://www.afugame.com/club/dispbbs.asp? Boardid = 13 & id = 5236 &
Page = 1 & move = next
Synchronized vs syncroot
We know that in some set types of. net, such as hashtable and arraylist, both have the synchronized static method and the syncroot instance side.
Are there any relationships between them? How can I make good use of them?
Let's take hashtable as an example to see their basic usage:
Hashtable ht = hashtable. synchronized (New hashtable ());
Lock
(HT. syncroot)
{
// Some operations
} 1. Synchronized indicates a thread-safe hashtable.
Is hashtable a thread safe? Next we will start to understand the source code of. net.
Public static hashtable
Synchronized (hashtable table)
{
If (Table = NULL)
{
Throw
New argumentnullexception ("table ");
}
Return new
Synchashtable (table );
}
Slave
The source code shows that the Synchronized Method returns a synchhashtable instance. As we said before, synchronized indicates that
Return to a thread-safe hashtable. From this explanation, synchhashtable should be inherited from hashtable. Let's verify it. Look
Source code of the synchhashtable type: Code
[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, 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;
}
}
}
}
Collapse Methods
Ah
As expected, synchashtable inherits from hashtable, and all synchashtable can implement thread security operations.
In some of our methods, lock is added. We know which thread executes the lock operation. Before the lock is released, other threads are blocked.
Synchashtable implements the so-called thread security through this method.
Now that we understand the meaning and usage of synchronized, let's take a look at its relationship with syncroot.
Syncroot indicates getting available
To be honest, this explanation is not easy to understand. To really understand its usage, we have to start from the source code: Public Virtual
Object syncroot
{
Get
{
If (this. _ syncroot = NULL)
{
Interlocked. compareexchange (ref
This. _ syncroot, new object (), null );
}
Return this. _ syncroot;
}
}
For example
If you are clear about the use of interlocked, this code is not hard to understand (if you are not clear about Google). Interlocked provides the original information for variables shared by multiple threads.
Sub-operation.
An atomic operation is a single-threaded operation. In a hashtable instance, no matter where the code is called, all objects of the same object type are returned. We are writing
Lock (HT. syncroot) works the same as the following operations.
Static object OBJ = new object ();
Lock (OBJ)
{
//
Some operations
}
The difference between them is that the declared static object type objects are of Type level, while syncroot is of object level.
Through the above analysis, we should be able to understand synchronized and syncroot usage. The relationship between them is:
Hashtable
Use the synchronized method to generate a synchhashtable object.
(This. _ table. syncroot) to implement thread-safe operations. Among them, this. _ table. syncroot returns
An object of the object type, in a synchhashtable object instance, is unique no matter how many calls we call.