Collection -> 集合的同步执行

using System;
using System.Collections;
using System.Collections.Specialized;

namespace 集合和同步
{
///

1<summary>   
2/// Class1 的摘要说明。   
3/// </summary>

class Class1
{
public static void DemoLockCollection()
{
StringDictionary sDic=new StringDictionary() ;
if(!sDic.IsSynchronized)
{
sDic.Add("晓华","花园路");
sDic["小杨"]="新兴桥";
foreach(string key in sDic.Keys)
Console.WriteLine("姓名={0},地址={1}",key,sDic[key]);
}
else
{
//不采取同步处理代码
}
}
[STAThread]
static void Main(string[] args)
{
//以下一行代码为类DemoLockCollection()的运行
Class1.DemoLockCollection();

//以下代码为另一个HashTable的例程
//创建并初始化一个哈希表
Hashtable myHT=new Hashtable();
myHT.Add(0,"zero");
myHT.Add(1,"one");
myHT.Add(2,"two");
myHT.Add(3,"three");
myHT.Add(4,"four");

//创建线程安全的包装
Hashtable mySyncdHT=Hashtable.Synchronized(myHT);

//显示哈萨克希表的同步的状态
Console.WriteLine("myHT{0}.",myHT.IsSynchronized ? "已经同步" : "没有同步");
Console.WriteLine("mySncdHT{0}.",mySyncdHT.IsSynchronized ? "已经同步" : "没有同步");

//遍历哈希
foreach(Object key in mySyncdHT.Keys)
Console.WriteLine("Key={0},Value={1]",key,mySyncdHT[key]);

Console.Read();
}
}
}

谢谢阅读!

Published At
Categories with Web编程
Tagged with
comments powered by Disqus