Hashtable的锁sample

要实现lock hashtable有两种方式:

1.锁SyncRoot:
第一步:申明同步HashTable-->Hashtable.Synchronized(new Hashtable());
第二步:第一次访问的时候,lock SyncRoot-->lock (this.table.SyncRoot)
当有其他线程访问这个被lock住的HashTable时,需要等待,直到lock释放.看一下示例代码:
 

ContractedBlock.gif ExpandedBlockStart.gif 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实例。代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 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.
}
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值