b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

C#多线程编程之:集合类中Synchronized方法与SyncRoot属性原理简析(转)

电脑杂谈  发布时间:2019-10-02 07:02:52  来源:网络整理

c# 多线程 syncroot_c++线程死锁_c++ 线程死锁

我们了解,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot属性,他们之间有联系吗?我如何能够用好他们呢?

以Hashtable为例,看看你们的基本用法:

1 Hashtable ht = Hashtable.Synchronized(new Hashtable());
2  lock (ht.SyncRoot)
3 {
4 ......
5 }

Synchronized表示返回一个线程安全的Hashtable,什么样的 hashtable才是一个线程安全的呢?下边我们就从.NET的源码开始理解。

c# 多线程 syncroot_c++线程死锁_c++ 线程死锁

1 public static Hashtable Synchronized(Hashtable table)
2 {
3 if (table == null)
4 {
5 throw new ArgumentNullException("table");
6 }
7 return new SyncHashtable(table);
8 }

从源码不难看出,Synchronized方法返回的或许是一个SynchHashtable类型的例子。在前面我们讲到,Synchronized表示返回一个线程安全的Hashtable,从这个解释不难看出,SynchHashtable应该是继承自Hashtable。下边我们验证一下。看看 SynchHashtable类型的源码:

1 [Serializable]
2 private class SyncHashtable : Hashtable
3 {
4 // Fields
5 protected Hashtable _table;
6
7 // Methods
8 internal SyncHashtable(Hashtable table)
9 : base(false)
10 {
11 this._table = table;
12 }
13
14 internal SyncHashtable(SerializationInfo info, StreamingContext context)
15 : base(info, context)
16 {
17 this._table = (Hashtable)info.GetValue("ParentTable", typeof(Hashtable));
18 if (this._table == null)
19 {
20 throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
21 }
22 }
23
24 public override void Add(object key, object value)
25 {
26 lock (this._table.SyncRoot)
27 {
28 this._table.Add(key, value);
29 }
30 }
31
32 public override void Clear()
33 {
34 lock (this._table.SyncRoot)
35 {
36 this._table.Clear();
37 }
38 }
39
40 public override object Clone()
41 {
42 lock (this._table.SyncRoot)
43 {
44 return Hashtable.Synchronized((Hashtable)this._table.Clone());
45 }
46 }
47
48 public override bool Contains(object key)
49 {
50 return this._table.Contains(key);
51 }
52
53 public override bool ContainsKey(object key)
54 {
55 return this._table.ContainsKey(key);
56 }
57
58 public override bool ContainsValue(object key)
59 {
60 lock (this._table.SyncRoot)
61 {
62 return this._table.ContainsValue(key);
63 }
64 }
65
66 public override void CopyTo(Array array, int arrayIndex)
67 {
68 lock (this._table.SyncRoot)
69 {
70 this._table.CopyTo(array, arrayIndex);
71 }
72 }
73
74 public override IDictionaryEnumerator GetEnumerator()
75 {
76 return this._table.GetEnumerator();
77 }
78
79 public override void GetObjectData(SerializationInfo info, StreamingContext context)
80 {
81 if (info == null)
82 {
83 throw new ArgumentNullException("info");
84 }
85 info.AddValue("ParentTable", this._table, typeof(Hashtable));
86 }
87
88 public override void OnDeserialization(object sender)
89 {
90 }
91
92 public override void Remove(object key)
93 {
94 lock (this._table.SyncRoot)
95 {
96 this._table.Remove(key);
97 }
98 }
99
100 internal override KeyValuePairs[] ToKeyValuePairsArray()
101 {
102 return this._table.ToKeyValuePairsArray();
103 }
104
105 // Properties
106 public override int Count
107 {
108 get
109 {
110 return this._table.Count;
111 }
112 }
113
114 public override bool IsFixedSize
115 {
116 get
117 {
118 return this._table.IsFixedSize;
119 }
120 }
121
122 public override bool IsReadOnly
123 {
124 get
125 {
126 return this._table.IsReadOnly;
127 }
128 }
129
130 public override bool IsSynchronized
131 {
132 get
133 {
134 return true;
135 }
136 }
137
138 public override object this[object key]
139 {
140 get
141 {
142 return this._table[key];
143 }
144 set
145 {
146 lock (this._table.SyncRoot)
147 {
148 this._table[key] = value;
149 }
150 }
151 }
152
153 public override ICollection Keys
154 {
155 get
156 {
157 lock (this._table.SyncRoot)
158 {
159 return this._table.Keys;
160 }
161 }
162 }
163
164 public override object SyncRoot
165 {
166 get
167 {
168 return this._table.SyncRoot;
169 }
170 }
171
172 public override ICollection Values
173 {
174 get
175 {
176 lock (this._table.SyncRoot)
177 {
178 return this._table.Values;
179 }
180 }
181 }
182 }


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-124838-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 吉野裕行
      吉野裕行

      现在的媒体都成了杀猪刀

    每日福利
    热点图片
    拼命载入中...