Hashtable的鎖sample

來源:互聯網
上載者:User

要實現lock hashtable有兩種方式:

1.鎖SyncRoot:
第一步:申明同步HashTable-->Hashtable.Synchronized(new Hashtable());
第二步:第一次訪問的時候,lock SyncRoot-->lock (this.table.SyncRoot)
當有其他線程訪問這個被lock住的HashTable時,需要等待,直到lock釋放.看一下範例程式碼: 

SyncRoot

public class HashTableLockSyncRoot
{
private Hashtable table;

public HashTableLockSyncRoot()
{
this.table = Hashtable.Synchronized(new Hashtable());
table.Add("K", "0");
}


public void ExecuteSample()
{
Thread T1 = new Thread(SynT1);
Thread T2 = new Thread(SynT2);

T1.Start();
T2.Start();

Thread.Sleep(5000);
Console.WriteLine("-----Main method-----");
Console.WriteLine(this.table["K"].ToString());

}

private void SynT1()
{
lock (this.table.SyncRoot)
{
Thread.Sleep(3000);

Console.WriteLine("-----Thread 1 before write-----");
Console.WriteLine(this.table["K"].ToString());

this.table["K"] = "1";

Console.WriteLine("-----Thread 1 after write-----");
Console.WriteLine(this.table["K"].ToString());
}


}

private void SynT2()
{

Console.WriteLine("-----Thread 2 before write-----");
Console.WriteLine(this.table["K"].ToString());

this.table["K"] = "2";

Console.WriteLine("-----Thread 2 after write-----");
Console.WriteLine(this.table["K"].ToString());

}

}

 

 

2.鎖執行個體
如果你要採用鎖執行個體的方式,那麼和上面的區別時,你在每次要訪問HashTable的時候,都需要lock HashTable執行個體。代碼如下:

Lock instance

public class HashTableLockInstance
{
private Hashtable table;

public HashTableLockInstance()
{
this.table = new Hashtable();
this.table.Add("K", "V0");
}


public void ExecuteSample()
{
Thread T1 = new Thread(SynT1);
Thread T2 = new Thread(SynT2);

T1.Start();
T2.Start();

Thread.Sleep(5000);

Console.WriteLine("----Main method-----");
Console.WriteLine(this.table["K"].ToString());

}

private void SynT1()
{
lock (this.table.SyncRoot)
{
Thread.Sleep(3000);

Console.WriteLine("-----Thread 1 before write-----");
Console.WriteLine(this.table["K"].ToString());


table["K"] = "V1";

Console.WriteLine("-----Thread 1 after write-----");
Console.WriteLine(this.table["K"].ToString());
}
}

private void SynT2()
{
lock (this.table.SyncRoot)
{
Console.WriteLine("-----Thread 2 before write-----");
Console.WriteLine(this.table["K"].ToString());

table["K"] = "V2";

Console.WriteLine("-----Thread 2 after write-----");
Console.WriteLine(this.table["K"].ToString());
}
}
}

 
3.兩者的區別:
SynRoot: 允許多個線程同時讀,只允許一個線程寫;而Lock instance則只允許一個線程同時讀寫。

 

補充:

(1). public virtual Object SyncRoot { get; }

To create a synchronized version of the ArrayList, use the Synchronized method. However, derived classes can provide their own synchronized version of the ArrayList using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the ArrayList, not directly on the ArrayList. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the ArrayList object. (也就是說只是阻止同步的修改,允許同步的讀取)

==============================================================================================================
(2). public static ArrayList Synchronized(
 ArrayList list
)
To guarantee the thread safety of the ArrayList, all operations must be done through this wrapper(只允許一個線程讀取).

==============================================================================================================
(3). Enumerating through a collection is intrinsically not a thread-safe procedure (枚舉讀取ArrayList是線程不安全的). Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
安全的讀取方式:

ArrayList myCollection = new ArrayList();
lock(myCollection.SyncRoot) {
foreach (Object item in myCollection) {
// Insert your code here.
}
}

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.