1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.36 |
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util.concurrent; |
8 |
dl |
1.10 |
import java.util.concurrent.locks.*; |
9 |
tim |
1.1 |
import java.util.*; |
10 |
|
|
import java.io.Serializable; |
11 |
|
|
import java.io.IOException; |
12 |
|
|
import java.io.ObjectInputStream; |
13 |
|
|
import java.io.ObjectOutputStream; |
14 |
|
|
|
15 |
|
|
/** |
16 |
dl |
1.4 |
* A hash table supporting full concurrency of retrievals and |
17 |
|
|
* adjustable expected concurrency for updates. This class obeys the |
18 |
dl |
1.22 |
* same functional specification as {@link java.util.Hashtable}, and |
19 |
dl |
1.19 |
* includes versions of methods corresponding to each method of |
20 |
dl |
1.25 |
* <tt>Hashtable</tt>. However, even though all operations are |
21 |
dl |
1.19 |
* thread-safe, retrieval operations do <em>not</em> entail locking, |
22 |
|
|
* and there is <em>not</em> any support for locking the entire table |
23 |
|
|
* in a way that prevents all access. This class is fully |
24 |
|
|
* interoperable with <tt>Hashtable</tt> in programs that rely on its |
25 |
dl |
1.4 |
* thread safety but not on its synchronization details. |
26 |
tim |
1.11 |
* |
27 |
dl |
1.25 |
* <p> Retrieval operations (including <tt>get</tt>) generally do not |
28 |
|
|
* block, so may overlap with update operations (including |
29 |
|
|
* <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results |
30 |
|
|
* of the most recently <em>completed</em> update operations holding |
31 |
|
|
* upon their onset. For aggregate operations such as <tt>putAll</tt> |
32 |
|
|
* and <tt>clear</tt>, concurrent retrievals may reflect insertion or |
33 |
dl |
1.4 |
* removal of only some entries. Similarly, Iterators and |
34 |
|
|
* Enumerations return elements reflecting the state of the hash table |
35 |
|
|
* at some point at or since the creation of the iterator/enumeration. |
36 |
dl |
1.25 |
* They do <em>not</em> throw |
37 |
dl |
1.28 |
* {@link ConcurrentModificationException}. However, iterators are |
38 |
dl |
1.25 |
* designed to be used by only one thread at a time. |
39 |
tim |
1.1 |
* |
40 |
dl |
1.19 |
* <p> The allowed concurrency among update operations is guided by |
41 |
|
|
* the optional <tt>concurrencyLevel</tt> constructor argument |
42 |
dl |
1.21 |
* (default 16), which is used as a hint for internal sizing. The |
43 |
|
|
* table is internally partitioned to try to permit the indicated |
44 |
|
|
* number of concurrent updates without contention. Because placement |
45 |
|
|
* in hash tables is essentially random, the actual concurrency will |
46 |
|
|
* vary. Ideally, you should choose a value to accommodate as many |
47 |
dl |
1.25 |
* threads as will ever concurrently modify the table. Using a |
48 |
dl |
1.21 |
* significantly higher value than you need can waste space and time, |
49 |
|
|
* and a significantly lower value can lead to thread contention. But |
50 |
|
|
* overestimates and underestimates within an order of magnitude do |
51 |
dl |
1.25 |
* not usually have much noticeable impact. A value of one is |
52 |
|
|
* appropriate when it is known that only one thread will modify |
53 |
|
|
* and all others will only read. |
54 |
tim |
1.1 |
* |
55 |
dl |
1.23 |
* <p>This class implements all of the <em>optional</em> methods |
56 |
|
|
* of the {@link Map} and {@link Iterator} interfaces. |
57 |
|
|
* |
58 |
dl |
1.22 |
* <p> Like {@link java.util.Hashtable} but unlike {@link |
59 |
|
|
* java.util.HashMap}, this class does NOT allow <tt>null</tt> to be |
60 |
|
|
* used as a key or value. |
61 |
tim |
1.1 |
* |
62 |
dl |
1.8 |
* @since 1.5 |
63 |
|
|
* @author Doug Lea |
64 |
dl |
1.27 |
* @param <K> the type of keys maintained by this map |
65 |
|
|
* @param <V> the type of mapped values |
66 |
dl |
1.8 |
*/ |
67 |
tim |
1.1 |
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> |
68 |
|
|
implements ConcurrentMap<K, V>, Cloneable, Serializable { |
69 |
dl |
1.20 |
private static final long serialVersionUID = 7249069246763182397L; |
70 |
tim |
1.1 |
|
71 |
|
|
/* |
72 |
dl |
1.4 |
* The basic strategy is to subdivide the table among Segments, |
73 |
|
|
* each of which itself is a concurrently readable hash table. |
74 |
|
|
*/ |
75 |
tim |
1.1 |
|
76 |
dl |
1.4 |
/* ---------------- Constants -------------- */ |
77 |
tim |
1.11 |
|
78 |
dl |
1.4 |
/** |
79 |
dl |
1.19 |
* The default initial number of table slots for this table. |
80 |
dl |
1.4 |
* Used when not otherwise specified in constructor. |
81 |
|
|
*/ |
82 |
tim |
1.11 |
private static int DEFAULT_INITIAL_CAPACITY = 16; |
83 |
tim |
1.1 |
|
84 |
|
|
/** |
85 |
dl |
1.4 |
* The maximum capacity, used if a higher value is implicitly |
86 |
|
|
* specified by either of the constructors with arguments. MUST |
87 |
dl |
1.21 |
* be a power of two <= 1<<30 to ensure that entries are indexible |
88 |
|
|
* using ints. |
89 |
dl |
1.4 |
*/ |
90 |
dl |
1.21 |
static final int MAXIMUM_CAPACITY = 1 << 30; |
91 |
tim |
1.11 |
|
92 |
tim |
1.1 |
/** |
93 |
dl |
1.4 |
* The default load factor for this table. Used when not |
94 |
|
|
* otherwise specified in constructor. |
95 |
|
|
*/ |
96 |
tim |
1.11 |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
97 |
tim |
1.1 |
|
98 |
|
|
/** |
99 |
dl |
1.4 |
* The default number of concurrency control segments. |
100 |
tim |
1.1 |
**/ |
101 |
dl |
1.4 |
private static final int DEFAULT_SEGMENTS = 16; |
102 |
tim |
1.1 |
|
103 |
dl |
1.21 |
/** |
104 |
dl |
1.37 |
* The maximum number of segments to allow; used to bound |
105 |
|
|
* constructor arguments. |
106 |
dl |
1.21 |
*/ |
107 |
|
|
private static final int MAX_SEGMENTS = 1 << 16; // slightly conservative |
108 |
|
|
|
109 |
dl |
1.4 |
/* ---------------- Fields -------------- */ |
110 |
tim |
1.1 |
|
111 |
|
|
/** |
112 |
dl |
1.9 |
* Mask value for indexing into segments. The upper bits of a |
113 |
|
|
* key's hash code are used to choose the segment. |
114 |
tim |
1.1 |
**/ |
115 |
dl |
1.4 |
private final int segmentMask; |
116 |
tim |
1.1 |
|
117 |
|
|
/** |
118 |
dl |
1.4 |
* Shift value for indexing within segments. |
119 |
tim |
1.1 |
**/ |
120 |
dl |
1.4 |
private final int segmentShift; |
121 |
tim |
1.1 |
|
122 |
|
|
/** |
123 |
dl |
1.4 |
* The segments, each of which is a specialized hash table |
124 |
tim |
1.1 |
*/ |
125 |
tim |
1.11 |
private final Segment[] segments; |
126 |
dl |
1.4 |
|
127 |
dl |
1.6 |
private transient Set<K> keySet; |
128 |
tim |
1.12 |
private transient Set<Map.Entry<K,V>> entrySet; |
129 |
dl |
1.6 |
private transient Collection<V> values; |
130 |
dl |
1.4 |
|
131 |
|
|
/* ---------------- Small Utilities -------------- */ |
132 |
tim |
1.1 |
|
133 |
|
|
/** |
134 |
tim |
1.11 |
* Return a hash code for non-null Object x. |
135 |
dl |
1.37 |
* Uses the same hash code spreader as most other java.util hash tables. |
136 |
dl |
1.8 |
* @param x the object serving as a key |
137 |
|
|
* @return the hash code |
138 |
tim |
1.1 |
*/ |
139 |
dl |
1.4 |
private static int hash(Object x) { |
140 |
|
|
int h = x.hashCode(); |
141 |
|
|
h += ~(h << 9); |
142 |
|
|
h ^= (h >>> 14); |
143 |
|
|
h += (h << 4); |
144 |
|
|
h ^= (h >>> 10); |
145 |
|
|
return h; |
146 |
|
|
} |
147 |
|
|
|
148 |
tim |
1.1 |
/** |
149 |
dl |
1.4 |
* Return the segment that should be used for key with given hash |
150 |
tim |
1.1 |
*/ |
151 |
dl |
1.4 |
private Segment<K,V> segmentFor(int hash) { |
152 |
tim |
1.12 |
return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask]; |
153 |
dl |
1.4 |
} |
154 |
tim |
1.1 |
|
155 |
dl |
1.4 |
/* ---------------- Inner Classes -------------- */ |
156 |
tim |
1.1 |
|
157 |
|
|
/** |
158 |
dl |
1.6 |
* Segments are specialized versions of hash tables. This |
159 |
dl |
1.4 |
* subclasses from ReentrantLock opportunistically, just to |
160 |
|
|
* simplify some locking and avoid separate construction. |
161 |
tim |
1.1 |
**/ |
162 |
dl |
1.8 |
private static final class Segment<K,V> extends ReentrantLock implements Serializable { |
163 |
dl |
1.4 |
/* |
164 |
|
|
* Segments maintain a table of entry lists that are ALWAYS |
165 |
|
|
* kept in a consistent state, so can be read without locking. |
166 |
|
|
* Next fields of nodes are immutable (final). All list |
167 |
|
|
* additions are performed at the front of each bin. This |
168 |
|
|
* makes it easy to check changes, and also fast to traverse. |
169 |
|
|
* When nodes would otherwise be changed, new nodes are |
170 |
|
|
* created to replace them. This works well for hash tables |
171 |
|
|
* since the bin lists tend to be short. (The average length |
172 |
|
|
* is less than two for the default load factor threshold.) |
173 |
|
|
* |
174 |
|
|
* Read operations can thus proceed without locking, but rely |
175 |
|
|
* on a memory barrier to ensure that completed write |
176 |
|
|
* operations performed by other threads are |
177 |
|
|
* noticed. Conveniently, the "count" field, tracking the |
178 |
|
|
* number of elements, can also serve as the volatile variable |
179 |
|
|
* providing proper read/write barriers. This is convenient |
180 |
|
|
* because this field needs to be read in many read operations |
181 |
dl |
1.19 |
* anyway. |
182 |
dl |
1.4 |
* |
183 |
|
|
* Implementors note. The basic rules for all this are: |
184 |
|
|
* |
185 |
|
|
* - All unsynchronized read operations must first read the |
186 |
|
|
* "count" field, and should not look at table entries if |
187 |
|
|
* it is 0. |
188 |
tim |
1.11 |
* |
189 |
dl |
1.4 |
* - All synchronized write operations should write to |
190 |
|
|
* the "count" field after updating. The operations must not |
191 |
|
|
* take any action that could even momentarily cause |
192 |
|
|
* a concurrent read operation to see inconsistent |
193 |
|
|
* data. This is made easier by the nature of the read |
194 |
|
|
* operations in Map. For example, no operation |
195 |
|
|
* can reveal that the table has grown but the threshold |
196 |
|
|
* has not yet been updated, so there are no atomicity |
197 |
|
|
* requirements for this with respect to reads. |
198 |
|
|
* |
199 |
|
|
* As a guide, all critical volatile reads and writes are marked |
200 |
|
|
* in code comments. |
201 |
|
|
*/ |
202 |
tim |
1.11 |
|
203 |
dl |
1.24 |
private static final long serialVersionUID = 2249069246763182397L; |
204 |
|
|
|
205 |
dl |
1.4 |
/** |
206 |
|
|
* The number of elements in this segment's region. |
207 |
|
|
**/ |
208 |
|
|
transient volatile int count; |
209 |
|
|
|
210 |
|
|
/** |
211 |
dl |
1.21 |
* Number of updates; used for checking lack of modifications |
212 |
|
|
* in bulk-read methods. |
213 |
|
|
*/ |
214 |
|
|
transient int modCount; |
215 |
|
|
|
216 |
|
|
/** |
217 |
dl |
1.4 |
* The table is rehashed when its size exceeds this threshold. |
218 |
|
|
* (The value of this field is always (int)(capacity * |
219 |
|
|
* loadFactor).) |
220 |
|
|
*/ |
221 |
dl |
1.8 |
private transient int threshold; |
222 |
dl |
1.4 |
|
223 |
|
|
/** |
224 |
|
|
* The per-segment table |
225 |
|
|
*/ |
226 |
tim |
1.11 |
transient HashEntry[] table; |
227 |
dl |
1.4 |
|
228 |
|
|
/** |
229 |
|
|
* The load factor for the hash table. Even though this value |
230 |
|
|
* is same for all segments, it is replicated to avoid needing |
231 |
|
|
* links to outer object. |
232 |
|
|
* @serial |
233 |
|
|
*/ |
234 |
|
|
private final float loadFactor; |
235 |
tim |
1.1 |
|
236 |
dl |
1.4 |
Segment(int initialCapacity, float lf) { |
237 |
|
|
loadFactor = lf; |
238 |
tim |
1.11 |
setTable(new HashEntry[initialCapacity]); |
239 |
dl |
1.4 |
} |
240 |
tim |
1.1 |
|
241 |
dl |
1.4 |
/** |
242 |
tim |
1.11 |
* Set table to new HashEntry array. |
243 |
dl |
1.4 |
* Call only while holding lock or in constructor. |
244 |
|
|
**/ |
245 |
tim |
1.11 |
private void setTable(HashEntry[] newTable) { |
246 |
dl |
1.4 |
table = newTable; |
247 |
|
|
threshold = (int)(newTable.length * loadFactor); |
248 |
|
|
count = count; // write-volatile |
249 |
tim |
1.11 |
} |
250 |
dl |
1.4 |
|
251 |
|
|
/* Specialized implementations of map methods */ |
252 |
tim |
1.11 |
|
253 |
dl |
1.29 |
V get(Object key, int hash) { |
254 |
dl |
1.4 |
if (count != 0) { // read-volatile |
255 |
tim |
1.11 |
HashEntry[] tab = table; |
256 |
dl |
1.9 |
int index = hash & (tab.length - 1); |
257 |
tim |
1.11 |
HashEntry<K,V> e = (HashEntry<K,V>) tab[index]; |
258 |
dl |
1.4 |
while (e != null) { |
259 |
tim |
1.11 |
if (e.hash == hash && key.equals(e.key)) |
260 |
dl |
1.4 |
return e.value; |
261 |
|
|
e = e.next; |
262 |
|
|
} |
263 |
|
|
} |
264 |
|
|
return null; |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
boolean containsKey(Object key, int hash) { |
268 |
|
|
if (count != 0) { // read-volatile |
269 |
tim |
1.11 |
HashEntry[] tab = table; |
270 |
dl |
1.9 |
int index = hash & (tab.length - 1); |
271 |
tim |
1.11 |
HashEntry<K,V> e = (HashEntry<K,V>) tab[index]; |
272 |
dl |
1.4 |
while (e != null) { |
273 |
tim |
1.11 |
if (e.hash == hash && key.equals(e.key)) |
274 |
dl |
1.4 |
return true; |
275 |
|
|
e = e.next; |
276 |
|
|
} |
277 |
|
|
} |
278 |
|
|
return false; |
279 |
|
|
} |
280 |
tim |
1.11 |
|
281 |
dl |
1.4 |
boolean containsValue(Object value) { |
282 |
|
|
if (count != 0) { // read-volatile |
283 |
tim |
1.11 |
HashEntry[] tab = table; |
284 |
dl |
1.4 |
int len = tab.length; |
285 |
tim |
1.11 |
for (int i = 0 ; i < len; i++) |
286 |
tim |
1.12 |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i] ; e != null ; e = e.next) |
287 |
dl |
1.4 |
if (value.equals(e.value)) |
288 |
|
|
return true; |
289 |
|
|
} |
290 |
|
|
return false; |
291 |
|
|
} |
292 |
|
|
|
293 |
dl |
1.31 |
boolean replace(K key, int hash, V oldValue, V newValue) { |
294 |
|
|
lock(); |
295 |
|
|
try { |
296 |
|
|
int c = count; |
297 |
|
|
HashEntry[] tab = table; |
298 |
|
|
int index = hash & (tab.length - 1); |
299 |
|
|
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
300 |
|
|
HashEntry<K,V> e = first; |
301 |
|
|
for (;;) { |
302 |
|
|
if (e == null) |
303 |
|
|
return false; |
304 |
|
|
if (e.hash == hash && key.equals(e.key)) |
305 |
|
|
break; |
306 |
|
|
e = e.next; |
307 |
|
|
} |
308 |
|
|
|
309 |
dl |
1.33 |
V v = e.value; |
310 |
|
|
if (v == null || !oldValue.equals(v)) |
311 |
|
|
return false; |
312 |
|
|
|
313 |
|
|
e.value = newValue; |
314 |
|
|
count = c; // write-volatile |
315 |
|
|
return true; |
316 |
|
|
|
317 |
|
|
} finally { |
318 |
|
|
unlock(); |
319 |
|
|
} |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
V replace(K key, int hash, V newValue) { |
323 |
|
|
lock(); |
324 |
|
|
try { |
325 |
|
|
int c = count; |
326 |
|
|
HashEntry[] tab = table; |
327 |
|
|
int index = hash & (tab.length - 1); |
328 |
|
|
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
329 |
|
|
HashEntry<K,V> e = first; |
330 |
|
|
for (;;) { |
331 |
|
|
if (e == null) |
332 |
|
|
return null; |
333 |
|
|
if (e.hash == hash && key.equals(e.key)) |
334 |
|
|
break; |
335 |
|
|
e = e.next; |
336 |
dl |
1.32 |
} |
337 |
dl |
1.31 |
|
338 |
dl |
1.33 |
V v = e.value; |
339 |
dl |
1.31 |
e.value = newValue; |
340 |
|
|
count = c; // write-volatile |
341 |
dl |
1.33 |
return v; |
342 |
dl |
1.31 |
|
343 |
|
|
} finally { |
344 |
|
|
unlock(); |
345 |
|
|
} |
346 |
|
|
} |
347 |
|
|
|
348 |
dl |
1.32 |
|
349 |
tim |
1.11 |
V put(K key, int hash, V value, boolean onlyIfAbsent) { |
350 |
dl |
1.4 |
lock(); |
351 |
|
|
try { |
352 |
dl |
1.9 |
int c = count; |
353 |
tim |
1.11 |
HashEntry[] tab = table; |
354 |
dl |
1.9 |
int index = hash & (tab.length - 1); |
355 |
tim |
1.11 |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
356 |
|
|
|
357 |
|
|
for (HashEntry<K,V> e = first; e != null; e = (HashEntry<K,V>) e.next) { |
358 |
dl |
1.9 |
if (e.hash == hash && key.equals(e.key)) { |
359 |
tim |
1.11 |
V oldValue = e.value; |
360 |
dl |
1.4 |
if (!onlyIfAbsent) |
361 |
|
|
e.value = value; |
362 |
dl |
1.21 |
++modCount; |
363 |
dl |
1.9 |
count = c; // write-volatile |
364 |
dl |
1.4 |
return oldValue; |
365 |
|
|
} |
366 |
|
|
} |
367 |
tim |
1.11 |
|
368 |
dl |
1.4 |
tab[index] = new HashEntry<K,V>(hash, key, value, first); |
369 |
dl |
1.21 |
++modCount; |
370 |
dl |
1.9 |
++c; |
371 |
|
|
count = c; // write-volatile |
372 |
tim |
1.11 |
if (c > threshold) |
373 |
dl |
1.9 |
setTable(rehash(tab)); |
374 |
dl |
1.4 |
return null; |
375 |
tim |
1.16 |
} finally { |
376 |
dl |
1.4 |
unlock(); |
377 |
|
|
} |
378 |
|
|
} |
379 |
|
|
|
380 |
tim |
1.11 |
private HashEntry[] rehash(HashEntry[] oldTable) { |
381 |
dl |
1.4 |
int oldCapacity = oldTable.length; |
382 |
|
|
if (oldCapacity >= MAXIMUM_CAPACITY) |
383 |
dl |
1.9 |
return oldTable; |
384 |
dl |
1.4 |
|
385 |
|
|
/* |
386 |
|
|
* Reclassify nodes in each list to new Map. Because we are |
387 |
|
|
* using power-of-two expansion, the elements from each bin |
388 |
|
|
* must either stay at same index, or move with a power of two |
389 |
|
|
* offset. We eliminate unnecessary node creation by catching |
390 |
|
|
* cases where old nodes can be reused because their next |
391 |
|
|
* fields won't change. Statistically, at the default |
392 |
dl |
1.29 |
* threshold, only about one-sixth of them need cloning when |
393 |
dl |
1.4 |
* a table doubles. The nodes they replace will be garbage |
394 |
|
|
* collectable as soon as they are no longer referenced by any |
395 |
|
|
* reader thread that may be in the midst of traversing table |
396 |
|
|
* right now. |
397 |
|
|
*/ |
398 |
tim |
1.11 |
|
399 |
|
|
HashEntry[] newTable = new HashEntry[oldCapacity << 1]; |
400 |
dl |
1.4 |
int sizeMask = newTable.length - 1; |
401 |
|
|
for (int i = 0; i < oldCapacity ; i++) { |
402 |
|
|
// We need to guarantee that any existing reads of old Map can |
403 |
tim |
1.11 |
// proceed. So we cannot yet null out each bin. |
404 |
tim |
1.12 |
HashEntry<K,V> e = (HashEntry<K,V>)oldTable[i]; |
405 |
tim |
1.11 |
|
406 |
dl |
1.4 |
if (e != null) { |
407 |
|
|
HashEntry<K,V> next = e.next; |
408 |
|
|
int idx = e.hash & sizeMask; |
409 |
tim |
1.11 |
|
410 |
dl |
1.4 |
// Single node on list |
411 |
tim |
1.11 |
if (next == null) |
412 |
dl |
1.4 |
newTable[idx] = e; |
413 |
tim |
1.11 |
|
414 |
|
|
else { |
415 |
dl |
1.4 |
// Reuse trailing consecutive sequence at same slot |
416 |
|
|
HashEntry<K,V> lastRun = e; |
417 |
|
|
int lastIdx = idx; |
418 |
tim |
1.11 |
for (HashEntry<K,V> last = next; |
419 |
|
|
last != null; |
420 |
dl |
1.4 |
last = last.next) { |
421 |
|
|
int k = last.hash & sizeMask; |
422 |
|
|
if (k != lastIdx) { |
423 |
|
|
lastIdx = k; |
424 |
|
|
lastRun = last; |
425 |
|
|
} |
426 |
|
|
} |
427 |
|
|
newTable[lastIdx] = lastRun; |
428 |
tim |
1.11 |
|
429 |
dl |
1.4 |
// Clone all remaining nodes |
430 |
|
|
for (HashEntry<K,V> p = e; p != lastRun; p = p.next) { |
431 |
|
|
int k = p.hash & sizeMask; |
432 |
tim |
1.11 |
newTable[k] = new HashEntry<K,V>(p.hash, |
433 |
|
|
p.key, |
434 |
|
|
p.value, |
435 |
|
|
(HashEntry<K,V>) newTable[k]); |
436 |
dl |
1.4 |
} |
437 |
|
|
} |
438 |
|
|
} |
439 |
|
|
} |
440 |
dl |
1.9 |
return newTable; |
441 |
dl |
1.4 |
} |
442 |
dl |
1.6 |
|
443 |
|
|
/** |
444 |
|
|
* Remove; match on key only if value null, else match both. |
445 |
|
|
*/ |
446 |
dl |
1.4 |
V remove(Object key, int hash, Object value) { |
447 |
tim |
1.11 |
lock(); |
448 |
dl |
1.4 |
try { |
449 |
dl |
1.9 |
int c = count; |
450 |
dl |
1.4 |
HashEntry[] tab = table; |
451 |
dl |
1.9 |
int index = hash & (tab.length - 1); |
452 |
tim |
1.12 |
HashEntry<K,V> first = (HashEntry<K,V>)tab[index]; |
453 |
tim |
1.11 |
|
454 |
dl |
1.4 |
HashEntry<K,V> e = first; |
455 |
dl |
1.9 |
for (;;) { |
456 |
dl |
1.4 |
if (e == null) |
457 |
|
|
return null; |
458 |
dl |
1.9 |
if (e.hash == hash && key.equals(e.key)) |
459 |
dl |
1.4 |
break; |
460 |
|
|
e = e.next; |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
V oldValue = e.value; |
464 |
|
|
if (value != null && !value.equals(oldValue)) |
465 |
|
|
return null; |
466 |
dl |
1.9 |
|
467 |
dl |
1.4 |
// All entries following removed node can stay in list, but |
468 |
dl |
1.29 |
// all preceding ones need to be cloned. |
469 |
dl |
1.4 |
HashEntry<K,V> newFirst = e.next; |
470 |
tim |
1.11 |
for (HashEntry<K,V> p = first; p != e; p = p.next) |
471 |
|
|
newFirst = new HashEntry<K,V>(p.hash, p.key, |
472 |
dl |
1.8 |
p.value, newFirst); |
473 |
dl |
1.4 |
tab[index] = newFirst; |
474 |
dl |
1.21 |
++modCount; |
475 |
dl |
1.9 |
count = c-1; // write-volatile |
476 |
|
|
return oldValue; |
477 |
tim |
1.16 |
} finally { |
478 |
dl |
1.4 |
unlock(); |
479 |
|
|
} |
480 |
|
|
} |
481 |
|
|
|
482 |
|
|
void clear() { |
483 |
|
|
lock(); |
484 |
|
|
try { |
485 |
tim |
1.11 |
HashEntry[] tab = table; |
486 |
|
|
for (int i = 0; i < tab.length ; i++) |
487 |
dl |
1.4 |
tab[i] = null; |
488 |
dl |
1.21 |
++modCount; |
489 |
dl |
1.4 |
count = 0; // write-volatile |
490 |
tim |
1.16 |
} finally { |
491 |
dl |
1.4 |
unlock(); |
492 |
|
|
} |
493 |
|
|
} |
494 |
tim |
1.1 |
} |
495 |
|
|
|
496 |
|
|
/** |
497 |
dl |
1.30 |
* ConcurrentHashMap list entry. Note that this is never exported |
498 |
|
|
* out as a user-visible Map.Entry |
499 |
tim |
1.1 |
*/ |
500 |
dl |
1.30 |
private static class HashEntry<K,V> { |
501 |
dl |
1.4 |
private final K key; |
502 |
|
|
private V value; |
503 |
|
|
private final int hash; |
504 |
|
|
private final HashEntry<K,V> next; |
505 |
|
|
|
506 |
|
|
HashEntry(int hash, K key, V value, HashEntry<K,V> next) { |
507 |
|
|
this.value = value; |
508 |
|
|
this.hash = hash; |
509 |
|
|
this.key = key; |
510 |
|
|
this.next = next; |
511 |
|
|
} |
512 |
tim |
1.1 |
} |
513 |
|
|
|
514 |
tim |
1.11 |
|
515 |
dl |
1.4 |
/* ---------------- Public operations -------------- */ |
516 |
tim |
1.1 |
|
517 |
|
|
/** |
518 |
|
|
* Constructs a new, empty map with the specified initial |
519 |
|
|
* capacity and the specified load factor. |
520 |
|
|
* |
521 |
dl |
1.19 |
* @param initialCapacity the initial capacity. The implementation |
522 |
|
|
* performs internal sizing to accommodate this many elements. |
523 |
tim |
1.1 |
* @param loadFactor the load factor threshold, used to control resizing. |
524 |
dl |
1.19 |
* @param concurrencyLevel the estimated number of concurrently |
525 |
|
|
* updating threads. The implementation performs internal sizing |
526 |
dl |
1.21 |
* to try to accommodate this many threads. |
527 |
dl |
1.4 |
* @throws IllegalArgumentException if the initial capacity is |
528 |
dl |
1.19 |
* negative or the load factor or concurrencyLevel are |
529 |
dl |
1.4 |
* nonpositive. |
530 |
|
|
*/ |
531 |
tim |
1.11 |
public ConcurrentHashMap(int initialCapacity, |
532 |
dl |
1.19 |
float loadFactor, int concurrencyLevel) { |
533 |
|
|
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0) |
534 |
dl |
1.4 |
throw new IllegalArgumentException(); |
535 |
|
|
|
536 |
dl |
1.21 |
if (concurrencyLevel > MAX_SEGMENTS) |
537 |
|
|
concurrencyLevel = MAX_SEGMENTS; |
538 |
|
|
|
539 |
dl |
1.4 |
// Find power-of-two sizes best matching arguments |
540 |
|
|
int sshift = 0; |
541 |
|
|
int ssize = 1; |
542 |
dl |
1.19 |
while (ssize < concurrencyLevel) { |
543 |
dl |
1.4 |
++sshift; |
544 |
|
|
ssize <<= 1; |
545 |
|
|
} |
546 |
dl |
1.9 |
segmentShift = 32 - sshift; |
547 |
dl |
1.8 |
segmentMask = ssize - 1; |
548 |
tim |
1.11 |
this.segments = new Segment[ssize]; |
549 |
dl |
1.4 |
|
550 |
|
|
if (initialCapacity > MAXIMUM_CAPACITY) |
551 |
|
|
initialCapacity = MAXIMUM_CAPACITY; |
552 |
|
|
int c = initialCapacity / ssize; |
553 |
tim |
1.11 |
if (c * ssize < initialCapacity) |
554 |
dl |
1.4 |
++c; |
555 |
|
|
int cap = 1; |
556 |
|
|
while (cap < c) |
557 |
|
|
cap <<= 1; |
558 |
|
|
|
559 |
|
|
for (int i = 0; i < this.segments.length; ++i) |
560 |
|
|
this.segments[i] = new Segment<K,V>(cap, loadFactor); |
561 |
tim |
1.1 |
} |
562 |
|
|
|
563 |
|
|
/** |
564 |
|
|
* Constructs a new, empty map with the specified initial |
565 |
dl |
1.19 |
* capacity, and with default load factor and concurrencyLevel. |
566 |
tim |
1.1 |
* |
567 |
dl |
1.19 |
* @param initialCapacity The implementation performs internal |
568 |
|
|
* sizing to accommodate this many elements. |
569 |
dl |
1.4 |
* @throws IllegalArgumentException if the initial capacity of |
570 |
|
|
* elements is negative. |
571 |
tim |
1.1 |
*/ |
572 |
|
|
public ConcurrentHashMap(int initialCapacity) { |
573 |
dl |
1.4 |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
574 |
tim |
1.1 |
} |
575 |
|
|
|
576 |
|
|
/** |
577 |
dl |
1.4 |
* Constructs a new, empty map with a default initial capacity, |
578 |
dl |
1.23 |
* load factor, and concurrencyLevel. |
579 |
tim |
1.1 |
*/ |
580 |
|
|
public ConcurrentHashMap() { |
581 |
dl |
1.4 |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
582 |
tim |
1.1 |
} |
583 |
|
|
|
584 |
|
|
/** |
585 |
|
|
* Constructs a new map with the same mappings as the given map. The |
586 |
|
|
* map is created with a capacity of twice the number of mappings in |
587 |
dl |
1.4 |
* the given map or 11 (whichever is greater), and a default load factor. |
588 |
tim |
1.1 |
*/ |
589 |
|
|
public <A extends K, B extends V> ConcurrentHashMap(Map<A,B> t) { |
590 |
|
|
this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1, |
591 |
dl |
1.4 |
11), |
592 |
|
|
DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
593 |
|
|
putAll(t); |
594 |
tim |
1.1 |
} |
595 |
|
|
|
596 |
dl |
1.4 |
// inherit Map javadoc |
597 |
tim |
1.1 |
public boolean isEmpty() { |
598 |
dl |
1.35 |
final Segment[] segments = this.segments; |
599 |
dl |
1.21 |
/* |
600 |
|
|
* We need to keep track of per-segment modCounts to avoid ABA |
601 |
|
|
* problems in which an element in one segment was added and |
602 |
|
|
* in another removed during traversal, in which case the |
603 |
|
|
* table was never actually empty at any point. Note the |
604 |
|
|
* similar use of modCounts in the size() and containsValue() |
605 |
|
|
* methods, which are the only other methods also susceptible |
606 |
|
|
* to ABA problems. |
607 |
|
|
*/ |
608 |
|
|
int[] mc = new int[segments.length]; |
609 |
|
|
int mcsum = 0; |
610 |
|
|
for (int i = 0; i < segments.length; ++i) { |
611 |
dl |
1.4 |
if (segments[i].count != 0) |
612 |
tim |
1.1 |
return false; |
613 |
dl |
1.21 |
else |
614 |
|
|
mcsum += mc[i] = segments[i].modCount; |
615 |
|
|
} |
616 |
|
|
// If mcsum happens to be zero, then we know we got a snapshot |
617 |
|
|
// before any modifications at all were made. This is |
618 |
|
|
// probably common enough to bother tracking. |
619 |
|
|
if (mcsum != 0) { |
620 |
|
|
for (int i = 0; i < segments.length; ++i) { |
621 |
|
|
if (segments[i].count != 0 || |
622 |
|
|
mc[i] != segments[i].modCount) |
623 |
|
|
return false; |
624 |
|
|
} |
625 |
|
|
} |
626 |
tim |
1.1 |
return true; |
627 |
|
|
} |
628 |
|
|
|
629 |
dl |
1.21 |
// inherit Map javadoc |
630 |
|
|
public int size() { |
631 |
dl |
1.35 |
final Segment[] segments = this.segments; |
632 |
dl |
1.21 |
int[] mc = new int[segments.length]; |
633 |
|
|
for (;;) { |
634 |
dl |
1.23 |
long sum = 0; |
635 |
dl |
1.21 |
int mcsum = 0; |
636 |
|
|
for (int i = 0; i < segments.length; ++i) { |
637 |
|
|
sum += segments[i].count; |
638 |
|
|
mcsum += mc[i] = segments[i].modCount; |
639 |
|
|
} |
640 |
|
|
int check = 0; |
641 |
|
|
if (mcsum != 0) { |
642 |
|
|
for (int i = 0; i < segments.length; ++i) { |
643 |
|
|
check += segments[i].count; |
644 |
|
|
if (mc[i] != segments[i].modCount) { |
645 |
|
|
check = -1; // force retry |
646 |
|
|
break; |
647 |
|
|
} |
648 |
|
|
} |
649 |
|
|
} |
650 |
dl |
1.23 |
if (check == sum) { |
651 |
|
|
if (sum > Integer.MAX_VALUE) |
652 |
|
|
return Integer.MAX_VALUE; |
653 |
|
|
else |
654 |
|
|
return (int)sum; |
655 |
|
|
} |
656 |
dl |
1.21 |
} |
657 |
|
|
} |
658 |
|
|
|
659 |
|
|
|
660 |
tim |
1.1 |
/** |
661 |
|
|
* Returns the value to which the specified key is mapped in this table. |
662 |
|
|
* |
663 |
|
|
* @param key a key in the table. |
664 |
|
|
* @return the value to which the key is mapped in this table; |
665 |
dl |
1.19 |
* <tt>null</tt> if the key is not mapped to any value in |
666 |
tim |
1.1 |
* this table. |
667 |
dl |
1.8 |
* @throws NullPointerException if the key is |
668 |
dl |
1.19 |
* <tt>null</tt>. |
669 |
tim |
1.1 |
*/ |
670 |
tim |
1.11 |
public V get(Object key) { |
671 |
dl |
1.4 |
int hash = hash(key); // throws NullPointerException if key null |
672 |
dl |
1.29 |
return segmentFor(hash).get(key, hash); |
673 |
tim |
1.1 |
} |
674 |
|
|
|
675 |
|
|
/** |
676 |
|
|
* Tests if the specified object is a key in this table. |
677 |
tim |
1.11 |
* |
678 |
tim |
1.1 |
* @param key possible key. |
679 |
dl |
1.19 |
* @return <tt>true</tt> if and only if the specified object |
680 |
tim |
1.11 |
* is a key in this table, as determined by the |
681 |
dl |
1.19 |
* <tt>equals</tt> method; <tt>false</tt> otherwise. |
682 |
dl |
1.8 |
* @throws NullPointerException if the key is |
683 |
dl |
1.19 |
* <tt>null</tt>. |
684 |
tim |
1.1 |
*/ |
685 |
|
|
public boolean containsKey(Object key) { |
686 |
dl |
1.4 |
int hash = hash(key); // throws NullPointerException if key null |
687 |
dl |
1.9 |
return segmentFor(hash).containsKey(key, hash); |
688 |
tim |
1.1 |
} |
689 |
|
|
|
690 |
|
|
/** |
691 |
|
|
* Returns <tt>true</tt> if this map maps one or more keys to the |
692 |
|
|
* specified value. Note: This method requires a full internal |
693 |
|
|
* traversal of the hash table, and so is much slower than |
694 |
|
|
* method <tt>containsKey</tt>. |
695 |
|
|
* |
696 |
|
|
* @param value value whose presence in this map is to be tested. |
697 |
|
|
* @return <tt>true</tt> if this map maps one or more keys to the |
698 |
tim |
1.11 |
* specified value. |
699 |
dl |
1.19 |
* @throws NullPointerException if the value is <tt>null</tt>. |
700 |
tim |
1.1 |
*/ |
701 |
|
|
public boolean containsValue(Object value) { |
702 |
tim |
1.11 |
if (value == null) |
703 |
dl |
1.4 |
throw new NullPointerException(); |
704 |
tim |
1.1 |
|
705 |
dl |
1.35 |
final Segment[] segments = this.segments; |
706 |
dl |
1.21 |
int[] mc = new int[segments.length]; |
707 |
|
|
for (;;) { |
708 |
|
|
int sum = 0; |
709 |
|
|
int mcsum = 0; |
710 |
|
|
for (int i = 0; i < segments.length; ++i) { |
711 |
|
|
int c = segments[i].count; |
712 |
|
|
mcsum += mc[i] = segments[i].modCount; |
713 |
|
|
if (segments[i].containsValue(value)) |
714 |
|
|
return true; |
715 |
|
|
} |
716 |
|
|
boolean cleanSweep = true; |
717 |
|
|
if (mcsum != 0) { |
718 |
|
|
for (int i = 0; i < segments.length; ++i) { |
719 |
|
|
int c = segments[i].count; |
720 |
|
|
if (mc[i] != segments[i].modCount) { |
721 |
|
|
cleanSweep = false; |
722 |
|
|
break; |
723 |
|
|
} |
724 |
|
|
} |
725 |
|
|
} |
726 |
|
|
if (cleanSweep) |
727 |
|
|
return false; |
728 |
tim |
1.1 |
} |
729 |
|
|
} |
730 |
dl |
1.19 |
|
731 |
tim |
1.1 |
/** |
732 |
dl |
1.18 |
* Legacy method testing if some key maps into the specified value |
733 |
dl |
1.23 |
* in this table. This method is identical in functionality to |
734 |
|
|
* {@link #containsValue}, and exists solely to ensure |
735 |
dl |
1.19 |
* full compatibility with class {@link java.util.Hashtable}, |
736 |
dl |
1.18 |
* which supported this method prior to introduction of the |
737 |
dl |
1.23 |
* Java Collections framework. |
738 |
dl |
1.17 |
|
739 |
tim |
1.1 |
* @param value a value to search for. |
740 |
dl |
1.19 |
* @return <tt>true</tt> if and only if some key maps to the |
741 |
|
|
* <tt>value</tt> argument in this table as |
742 |
tim |
1.1 |
* determined by the <tt>equals</tt> method; |
743 |
dl |
1.19 |
* <tt>false</tt> otherwise. |
744 |
|
|
* @throws NullPointerException if the value is <tt>null</tt>. |
745 |
tim |
1.1 |
*/ |
746 |
dl |
1.4 |
public boolean contains(Object value) { |
747 |
tim |
1.1 |
return containsValue(value); |
748 |
|
|
} |
749 |
|
|
|
750 |
|
|
/** |
751 |
dl |
1.19 |
* Maps the specified <tt>key</tt> to the specified |
752 |
|
|
* <tt>value</tt> in this table. Neither the key nor the |
753 |
|
|
* value can be <tt>null</tt>. <p> |
754 |
dl |
1.4 |
* |
755 |
dl |
1.19 |
* The value can be retrieved by calling the <tt>get</tt> method |
756 |
tim |
1.11 |
* with a key that is equal to the original key. |
757 |
dl |
1.4 |
* |
758 |
|
|
* @param key the table key. |
759 |
|
|
* @param value the value. |
760 |
|
|
* @return the previous value of the specified key in this table, |
761 |
dl |
1.19 |
* or <tt>null</tt> if it did not have one. |
762 |
dl |
1.8 |
* @throws NullPointerException if the key or value is |
763 |
dl |
1.19 |
* <tt>null</tt>. |
764 |
dl |
1.4 |
*/ |
765 |
tim |
1.11 |
public V put(K key, V value) { |
766 |
|
|
if (value == null) |
767 |
dl |
1.4 |
throw new NullPointerException(); |
768 |
tim |
1.11 |
int hash = hash(key); |
769 |
dl |
1.9 |
return segmentFor(hash).put(key, hash, value, false); |
770 |
dl |
1.4 |
} |
771 |
|
|
|
772 |
|
|
/** |
773 |
|
|
* If the specified key is not already associated |
774 |
|
|
* with a value, associate it with the given value. |
775 |
|
|
* This is equivalent to |
776 |
|
|
* <pre> |
777 |
dl |
1.17 |
* if (!map.containsKey(key)) |
778 |
|
|
* return map.put(key, value); |
779 |
|
|
* else |
780 |
|
|
* return map.get(key); |
781 |
dl |
1.4 |
* </pre> |
782 |
|
|
* Except that the action is performed atomically. |
783 |
|
|
* @param key key with which the specified value is to be associated. |
784 |
|
|
* @param value value to be associated with the specified key. |
785 |
|
|
* @return previous value associated with specified key, or <tt>null</tt> |
786 |
|
|
* if there was no mapping for key. A <tt>null</tt> return can |
787 |
|
|
* also indicate that the map previously associated <tt>null</tt> |
788 |
|
|
* with the specified key, if the implementation supports |
789 |
|
|
* <tt>null</tt> values. |
790 |
|
|
* |
791 |
dl |
1.17 |
* @throws UnsupportedOperationException if the <tt>put</tt> operation is |
792 |
|
|
* not supported by this map. |
793 |
|
|
* @throws ClassCastException if the class of the specified key or value |
794 |
|
|
* prevents it from being stored in this map. |
795 |
|
|
* @throws NullPointerException if the specified key or value is |
796 |
dl |
1.4 |
* <tt>null</tt>. |
797 |
|
|
* |
798 |
|
|
**/ |
799 |
tim |
1.11 |
public V putIfAbsent(K key, V value) { |
800 |
|
|
if (value == null) |
801 |
dl |
1.4 |
throw new NullPointerException(); |
802 |
tim |
1.11 |
int hash = hash(key); |
803 |
dl |
1.9 |
return segmentFor(hash).put(key, hash, value, true); |
804 |
dl |
1.4 |
} |
805 |
|
|
|
806 |
|
|
|
807 |
|
|
/** |
808 |
tim |
1.1 |
* Copies all of the mappings from the specified map to this one. |
809 |
|
|
* |
810 |
|
|
* These mappings replace any mappings that this map had for any of the |
811 |
|
|
* keys currently in the specified Map. |
812 |
|
|
* |
813 |
|
|
* @param t Mappings to be stored in this map. |
814 |
|
|
*/ |
815 |
tim |
1.11 |
public void putAll(Map<? extends K, ? extends V> t) { |
816 |
dl |
1.23 |
for (Iterator<Map.Entry<? extends K, ? extends V>> it = (Iterator<Map.Entry<? extends K, ? extends V>>) t.entrySet().iterator(); it.hasNext(); ) { |
817 |
tim |
1.12 |
Entry<? extends K, ? extends V> e = it.next(); |
818 |
dl |
1.4 |
put(e.getKey(), e.getValue()); |
819 |
tim |
1.1 |
} |
820 |
dl |
1.4 |
} |
821 |
|
|
|
822 |
|
|
/** |
823 |
tim |
1.11 |
* Removes the key (and its corresponding value) from this |
824 |
dl |
1.4 |
* table. This method does nothing if the key is not in the table. |
825 |
|
|
* |
826 |
|
|
* @param key the key that needs to be removed. |
827 |
|
|
* @return the value to which the key had been mapped in this table, |
828 |
dl |
1.19 |
* or <tt>null</tt> if the key did not have a mapping. |
829 |
dl |
1.8 |
* @throws NullPointerException if the key is |
830 |
dl |
1.19 |
* <tt>null</tt>. |
831 |
dl |
1.4 |
*/ |
832 |
|
|
public V remove(Object key) { |
833 |
|
|
int hash = hash(key); |
834 |
dl |
1.9 |
return segmentFor(hash).remove(key, hash, null); |
835 |
dl |
1.4 |
} |
836 |
tim |
1.1 |
|
837 |
dl |
1.4 |
/** |
838 |
dl |
1.17 |
* Remove entry for key only if currently mapped to given value. |
839 |
|
|
* Acts as |
840 |
|
|
* <pre> |
841 |
|
|
* if (map.get(key).equals(value)) { |
842 |
|
|
* map.remove(key); |
843 |
|
|
* return true; |
844 |
|
|
* } else return false; |
845 |
|
|
* </pre> |
846 |
|
|
* except that the action is performed atomically. |
847 |
|
|
* @param key key with which the specified value is associated. |
848 |
|
|
* @param value value associated with the specified key. |
849 |
|
|
* @return true if the value was removed |
850 |
|
|
* @throws NullPointerException if the specified key is |
851 |
|
|
* <tt>null</tt>. |
852 |
dl |
1.4 |
*/ |
853 |
dl |
1.13 |
public boolean remove(Object key, Object value) { |
854 |
dl |
1.4 |
int hash = hash(key); |
855 |
dl |
1.13 |
return segmentFor(hash).remove(key, hash, value) != null; |
856 |
tim |
1.1 |
} |
857 |
dl |
1.31 |
|
858 |
dl |
1.32 |
|
859 |
dl |
1.31 |
/** |
860 |
|
|
* Replace entry for key only if currently mapped to given value. |
861 |
|
|
* Acts as |
862 |
|
|
* <pre> |
863 |
|
|
* if (map.get(key).equals(oldValue)) { |
864 |
|
|
* map.put(key, newValue); |
865 |
|
|
* return true; |
866 |
|
|
* } else return false; |
867 |
|
|
* </pre> |
868 |
|
|
* except that the action is performed atomically. |
869 |
|
|
* @param key key with which the specified value is associated. |
870 |
|
|
* @param oldValue value expected to be associated with the specified key. |
871 |
|
|
* @param newValue value to be associated with the specified key. |
872 |
|
|
* @return true if the value was replaced |
873 |
|
|
* @throws NullPointerException if the specified key or values are |
874 |
|
|
* <tt>null</tt>. |
875 |
|
|
*/ |
876 |
|
|
public boolean replace(K key, V oldValue, V newValue) { |
877 |
|
|
if (oldValue == null || newValue == null) |
878 |
|
|
throw new NullPointerException(); |
879 |
|
|
int hash = hash(key); |
880 |
|
|
return segmentFor(hash).replace(key, hash, oldValue, newValue); |
881 |
dl |
1.32 |
} |
882 |
|
|
|
883 |
|
|
/** |
884 |
dl |
1.33 |
* Replace entry for key only if currently mapped to some value. |
885 |
dl |
1.32 |
* Acts as |
886 |
|
|
* <pre> |
887 |
|
|
* if ((map.containsKey(key)) { |
888 |
dl |
1.33 |
* return map.put(key, value); |
889 |
|
|
* } else return null; |
890 |
dl |
1.32 |
* </pre> |
891 |
|
|
* except that the action is performed atomically. |
892 |
|
|
* @param key key with which the specified value is associated. |
893 |
|
|
* @param value value to be associated with the specified key. |
894 |
dl |
1.33 |
* @return previous value associated with specified key, or <tt>null</tt> |
895 |
|
|
* if there was no mapping for key. |
896 |
dl |
1.32 |
* @throws NullPointerException if the specified key or value is |
897 |
|
|
* <tt>null</tt>. |
898 |
|
|
*/ |
899 |
dl |
1.33 |
public V replace(K key, V value) { |
900 |
dl |
1.32 |
if (value == null) |
901 |
|
|
throw new NullPointerException(); |
902 |
|
|
int hash = hash(key); |
903 |
dl |
1.33 |
return segmentFor(hash).replace(key, hash, value); |
904 |
dl |
1.31 |
} |
905 |
|
|
|
906 |
tim |
1.1 |
|
907 |
|
|
/** |
908 |
|
|
* Removes all mappings from this map. |
909 |
|
|
*/ |
910 |
|
|
public void clear() { |
911 |
tim |
1.11 |
for (int i = 0; i < segments.length; ++i) |
912 |
dl |
1.4 |
segments[i].clear(); |
913 |
tim |
1.1 |
} |
914 |
|
|
|
915 |
dl |
1.4 |
|
916 |
tim |
1.1 |
/** |
917 |
|
|
* Returns a shallow copy of this |
918 |
|
|
* <tt>ConcurrentHashMap</tt> instance: the keys and |
919 |
|
|
* values themselves are not cloned. |
920 |
|
|
* |
921 |
|
|
* @return a shallow copy of this map. |
922 |
|
|
*/ |
923 |
|
|
public Object clone() { |
924 |
dl |
1.4 |
// We cannot call super.clone, since it would share final |
925 |
|
|
// segments array, and there's no way to reassign finals. |
926 |
|
|
|
927 |
|
|
float lf = segments[0].loadFactor; |
928 |
|
|
int segs = segments.length; |
929 |
|
|
int cap = (int)(size() / lf); |
930 |
|
|
if (cap < segs) cap = segs; |
931 |
tim |
1.12 |
ConcurrentHashMap<K,V> t = new ConcurrentHashMap<K,V>(cap, lf, segs); |
932 |
dl |
1.4 |
t.putAll(this); |
933 |
|
|
return t; |
934 |
tim |
1.1 |
} |
935 |
|
|
|
936 |
|
|
/** |
937 |
|
|
* Returns a set view of the keys contained in this map. The set is |
938 |
|
|
* backed by the map, so changes to the map are reflected in the set, and |
939 |
|
|
* vice-versa. The set supports element removal, which removes the |
940 |
|
|
* corresponding mapping from this map, via the <tt>Iterator.remove</tt>, |
941 |
|
|
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and |
942 |
|
|
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
943 |
|
|
* <tt>addAll</tt> operations. |
944 |
dl |
1.14 |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
945 |
|
|
* will never throw {@link java.util.ConcurrentModificationException}, |
946 |
|
|
* and guarantees to traverse elements as they existed upon |
947 |
|
|
* construction of the iterator, and may (but is not guaranteed to) |
948 |
|
|
* reflect any modifications subsequent to construction. |
949 |
tim |
1.1 |
* |
950 |
|
|
* @return a set view of the keys contained in this map. |
951 |
|
|
*/ |
952 |
|
|
public Set<K> keySet() { |
953 |
|
|
Set<K> ks = keySet; |
954 |
dl |
1.8 |
return (ks != null) ? ks : (keySet = new KeySet()); |
955 |
tim |
1.1 |
} |
956 |
|
|
|
957 |
|
|
|
958 |
|
|
/** |
959 |
|
|
* Returns a collection view of the values contained in this map. The |
960 |
|
|
* collection is backed by the map, so changes to the map are reflected in |
961 |
|
|
* the collection, and vice-versa. The collection supports element |
962 |
|
|
* removal, which removes the corresponding mapping from this map, via the |
963 |
|
|
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
964 |
|
|
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
965 |
|
|
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
966 |
dl |
1.14 |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
967 |
|
|
* will never throw {@link java.util.ConcurrentModificationException}, |
968 |
|
|
* and guarantees to traverse elements as they existed upon |
969 |
|
|
* construction of the iterator, and may (but is not guaranteed to) |
970 |
|
|
* reflect any modifications subsequent to construction. |
971 |
tim |
1.1 |
* |
972 |
|
|
* @return a collection view of the values contained in this map. |
973 |
|
|
*/ |
974 |
|
|
public Collection<V> values() { |
975 |
|
|
Collection<V> vs = values; |
976 |
dl |
1.8 |
return (vs != null) ? vs : (values = new Values()); |
977 |
tim |
1.1 |
} |
978 |
|
|
|
979 |
|
|
|
980 |
|
|
/** |
981 |
|
|
* Returns a collection view of the mappings contained in this map. Each |
982 |
|
|
* element in the returned collection is a <tt>Map.Entry</tt>. The |
983 |
|
|
* collection is backed by the map, so changes to the map are reflected in |
984 |
|
|
* the collection, and vice-versa. The collection supports element |
985 |
|
|
* removal, which removes the corresponding mapping from the map, via the |
986 |
|
|
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
987 |
|
|
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
988 |
|
|
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
989 |
dl |
1.14 |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
990 |
|
|
* will never throw {@link java.util.ConcurrentModificationException}, |
991 |
|
|
* and guarantees to traverse elements as they existed upon |
992 |
|
|
* construction of the iterator, and may (but is not guaranteed to) |
993 |
|
|
* reflect any modifications subsequent to construction. |
994 |
tim |
1.1 |
* |
995 |
|
|
* @return a collection view of the mappings contained in this map. |
996 |
|
|
*/ |
997 |
|
|
public Set<Map.Entry<K,V>> entrySet() { |
998 |
|
|
Set<Map.Entry<K,V>> es = entrySet; |
999 |
dl |
1.23 |
return (es != null) ? es : (entrySet = (Set<Map.Entry<K,V>>) (Set) new EntrySet()); |
1000 |
tim |
1.1 |
} |
1001 |
|
|
|
1002 |
|
|
|
1003 |
|
|
/** |
1004 |
|
|
* Returns an enumeration of the keys in this table. |
1005 |
|
|
* |
1006 |
|
|
* @return an enumeration of the keys in this table. |
1007 |
dl |
1.23 |
* @see #keySet |
1008 |
tim |
1.1 |
*/ |
1009 |
dl |
1.4 |
public Enumeration<K> keys() { |
1010 |
tim |
1.1 |
return new KeyIterator(); |
1011 |
|
|
} |
1012 |
|
|
|
1013 |
|
|
/** |
1014 |
|
|
* Returns an enumeration of the values in this table. |
1015 |
|
|
* Use the Enumeration methods on the returned object to fetch the elements |
1016 |
|
|
* sequentially. |
1017 |
|
|
* |
1018 |
|
|
* @return an enumeration of the values in this table. |
1019 |
dl |
1.23 |
* @see #values |
1020 |
tim |
1.1 |
*/ |
1021 |
dl |
1.4 |
public Enumeration<V> elements() { |
1022 |
tim |
1.1 |
return new ValueIterator(); |
1023 |
|
|
} |
1024 |
|
|
|
1025 |
dl |
1.4 |
/* ---------------- Iterator Support -------------- */ |
1026 |
tim |
1.11 |
|
1027 |
dl |
1.4 |
private abstract class HashIterator { |
1028 |
|
|
private int nextSegmentIndex; |
1029 |
|
|
private int nextTableIndex; |
1030 |
tim |
1.11 |
private HashEntry[] currentTable; |
1031 |
dl |
1.4 |
private HashEntry<K, V> nextEntry; |
1032 |
dl |
1.30 |
HashEntry<K, V> lastReturned; |
1033 |
tim |
1.1 |
|
1034 |
|
|
private HashIterator() { |
1035 |
dl |
1.8 |
nextSegmentIndex = segments.length - 1; |
1036 |
dl |
1.4 |
nextTableIndex = -1; |
1037 |
|
|
advance(); |
1038 |
tim |
1.1 |
} |
1039 |
|
|
|
1040 |
|
|
public boolean hasMoreElements() { return hasNext(); } |
1041 |
|
|
|
1042 |
dl |
1.4 |
private void advance() { |
1043 |
|
|
if (nextEntry != null && (nextEntry = nextEntry.next) != null) |
1044 |
|
|
return; |
1045 |
tim |
1.11 |
|
1046 |
dl |
1.4 |
while (nextTableIndex >= 0) { |
1047 |
tim |
1.12 |
if ( (nextEntry = (HashEntry<K,V>)currentTable[nextTableIndex--]) != null) |
1048 |
dl |
1.4 |
return; |
1049 |
|
|
} |
1050 |
tim |
1.11 |
|
1051 |
dl |
1.4 |
while (nextSegmentIndex >= 0) { |
1052 |
tim |
1.12 |
Segment<K,V> seg = (Segment<K,V>)segments[nextSegmentIndex--]; |
1053 |
dl |
1.4 |
if (seg.count != 0) { |
1054 |
|
|
currentTable = seg.table; |
1055 |
dl |
1.8 |
for (int j = currentTable.length - 1; j >= 0; --j) { |
1056 |
tim |
1.12 |
if ( (nextEntry = (HashEntry<K,V>)currentTable[j]) != null) { |
1057 |
dl |
1.8 |
nextTableIndex = j - 1; |
1058 |
dl |
1.4 |
return; |
1059 |
|
|
} |
1060 |
tim |
1.1 |
} |
1061 |
|
|
} |
1062 |
|
|
} |
1063 |
|
|
} |
1064 |
|
|
|
1065 |
dl |
1.4 |
public boolean hasNext() { return nextEntry != null; } |
1066 |
tim |
1.1 |
|
1067 |
dl |
1.4 |
HashEntry<K,V> nextEntry() { |
1068 |
|
|
if (nextEntry == null) |
1069 |
tim |
1.1 |
throw new NoSuchElementException(); |
1070 |
dl |
1.4 |
lastReturned = nextEntry; |
1071 |
|
|
advance(); |
1072 |
|
|
return lastReturned; |
1073 |
tim |
1.1 |
} |
1074 |
|
|
|
1075 |
|
|
public void remove() { |
1076 |
|
|
if (lastReturned == null) |
1077 |
|
|
throw new IllegalStateException(); |
1078 |
|
|
ConcurrentHashMap.this.remove(lastReturned.key); |
1079 |
|
|
lastReturned = null; |
1080 |
|
|
} |
1081 |
dl |
1.4 |
} |
1082 |
|
|
|
1083 |
|
|
private class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> { |
1084 |
|
|
public K next() { return super.nextEntry().key; } |
1085 |
|
|
public K nextElement() { return super.nextEntry().key; } |
1086 |
|
|
} |
1087 |
|
|
|
1088 |
|
|
private class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> { |
1089 |
|
|
public V next() { return super.nextEntry().value; } |
1090 |
|
|
public V nextElement() { return super.nextEntry().value; } |
1091 |
|
|
} |
1092 |
tim |
1.1 |
|
1093 |
dl |
1.30 |
|
1094 |
|
|
|
1095 |
|
|
/** |
1096 |
|
|
* Exported Entry objects must write-through changes in setValue, |
1097 |
|
|
* even if the nodes have been cloned. So we cannot return |
1098 |
|
|
* internal HashEntry objects. Instead, the iterator itself acts |
1099 |
|
|
* as a forwarding pseudo-entry. |
1100 |
|
|
*/ |
1101 |
|
|
private class EntryIterator extends HashIterator implements Map.Entry<K,V>, Iterator<Entry<K,V>> { |
1102 |
|
|
public Map.Entry<K,V> next() { |
1103 |
|
|
nextEntry(); |
1104 |
|
|
return this; |
1105 |
|
|
} |
1106 |
|
|
|
1107 |
|
|
public K getKey() { |
1108 |
|
|
if (lastReturned == null) |
1109 |
|
|
throw new IllegalStateException("Entry was removed"); |
1110 |
|
|
return lastReturned.key; |
1111 |
|
|
} |
1112 |
|
|
|
1113 |
|
|
public V getValue() { |
1114 |
|
|
if (lastReturned == null) |
1115 |
|
|
throw new IllegalStateException("Entry was removed"); |
1116 |
|
|
return ConcurrentHashMap.this.get(lastReturned.key); |
1117 |
|
|
} |
1118 |
|
|
|
1119 |
|
|
public V setValue(V value) { |
1120 |
|
|
if (lastReturned == null) |
1121 |
|
|
throw new IllegalStateException("Entry was removed"); |
1122 |
|
|
return ConcurrentHashMap.this.put(lastReturned.key, value); |
1123 |
|
|
} |
1124 |
|
|
|
1125 |
|
|
public boolean equals(Object o) { |
1126 |
|
|
if (!(o instanceof Map.Entry)) |
1127 |
|
|
return false; |
1128 |
|
|
Map.Entry e = (Map.Entry)o; |
1129 |
|
|
return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue()); |
1130 |
|
|
} |
1131 |
|
|
|
1132 |
|
|
public int hashCode() { |
1133 |
|
|
Object k = getKey(); |
1134 |
|
|
Object v = getValue(); |
1135 |
|
|
return ((k == null) ? 0 : k.hashCode()) ^ |
1136 |
|
|
((v == null) ? 0 : v.hashCode()); |
1137 |
|
|
} |
1138 |
|
|
|
1139 |
|
|
public String toString() { |
1140 |
dl |
1.34 |
// If not acting as entry, just use default toString. |
1141 |
|
|
if (lastReturned == null) |
1142 |
|
|
return super.toString(); |
1143 |
|
|
else |
1144 |
|
|
return getKey() + "=" + getValue(); |
1145 |
dl |
1.30 |
} |
1146 |
|
|
|
1147 |
|
|
private boolean eq(Object o1, Object o2) { |
1148 |
|
|
return (o1 == null ? o2 == null : o1.equals(o2)); |
1149 |
|
|
} |
1150 |
|
|
|
1151 |
tim |
1.1 |
} |
1152 |
|
|
|
1153 |
dl |
1.4 |
private class KeySet extends AbstractSet<K> { |
1154 |
|
|
public Iterator<K> iterator() { |
1155 |
|
|
return new KeyIterator(); |
1156 |
|
|
} |
1157 |
|
|
public int size() { |
1158 |
|
|
return ConcurrentHashMap.this.size(); |
1159 |
|
|
} |
1160 |
|
|
public boolean contains(Object o) { |
1161 |
|
|
return ConcurrentHashMap.this.containsKey(o); |
1162 |
|
|
} |
1163 |
|
|
public boolean remove(Object o) { |
1164 |
|
|
return ConcurrentHashMap.this.remove(o) != null; |
1165 |
|
|
} |
1166 |
|
|
public void clear() { |
1167 |
|
|
ConcurrentHashMap.this.clear(); |
1168 |
|
|
} |
1169 |
tim |
1.1 |
} |
1170 |
|
|
|
1171 |
dl |
1.4 |
private class Values extends AbstractCollection<V> { |
1172 |
|
|
public Iterator<V> iterator() { |
1173 |
|
|
return new ValueIterator(); |
1174 |
|
|
} |
1175 |
|
|
public int size() { |
1176 |
|
|
return ConcurrentHashMap.this.size(); |
1177 |
|
|
} |
1178 |
|
|
public boolean contains(Object o) { |
1179 |
|
|
return ConcurrentHashMap.this.containsValue(o); |
1180 |
|
|
} |
1181 |
|
|
public void clear() { |
1182 |
|
|
ConcurrentHashMap.this.clear(); |
1183 |
|
|
} |
1184 |
tim |
1.1 |
} |
1185 |
|
|
|
1186 |
tim |
1.12 |
private class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
1187 |
dl |
1.4 |
public Iterator<Map.Entry<K,V>> iterator() { |
1188 |
|
|
return new EntryIterator(); |
1189 |
|
|
} |
1190 |
|
|
public boolean contains(Object o) { |
1191 |
|
|
if (!(o instanceof Map.Entry)) |
1192 |
|
|
return false; |
1193 |
|
|
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
1194 |
|
|
V v = ConcurrentHashMap.this.get(e.getKey()); |
1195 |
|
|
return v != null && v.equals(e.getValue()); |
1196 |
|
|
} |
1197 |
|
|
public boolean remove(Object o) { |
1198 |
|
|
if (!(o instanceof Map.Entry)) |
1199 |
|
|
return false; |
1200 |
|
|
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
1201 |
dl |
1.13 |
return ConcurrentHashMap.this.remove(e.getKey(), e.getValue()); |
1202 |
dl |
1.4 |
} |
1203 |
|
|
public int size() { |
1204 |
|
|
return ConcurrentHashMap.this.size(); |
1205 |
|
|
} |
1206 |
|
|
public void clear() { |
1207 |
|
|
ConcurrentHashMap.this.clear(); |
1208 |
dl |
1.30 |
} |
1209 |
|
|
public Object[] toArray() { |
1210 |
|
|
// Since we don't ordinarily have distinct Entry objects, we |
1211 |
|
|
// must pack elements using exportable SimpleEntry |
1212 |
|
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1213 |
|
|
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1214 |
|
|
c.add(new SimpleEntry<K,V>(i.next())); |
1215 |
|
|
return c.toArray(); |
1216 |
|
|
} |
1217 |
|
|
public <T> T[] toArray(T[] a) { |
1218 |
|
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1219 |
|
|
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1220 |
|
|
c.add(new SimpleEntry<K,V>(i.next())); |
1221 |
|
|
return c.toArray(a); |
1222 |
|
|
} |
1223 |
|
|
|
1224 |
|
|
} |
1225 |
|
|
|
1226 |
|
|
/** |
1227 |
|
|
* This duplicates java.util.AbstractMap.SimpleEntry until this class |
1228 |
|
|
* is made accessible. |
1229 |
|
|
*/ |
1230 |
|
|
static class SimpleEntry<K,V> implements Entry<K,V> { |
1231 |
|
|
K key; |
1232 |
|
|
V value; |
1233 |
|
|
|
1234 |
|
|
public SimpleEntry(K key, V value) { |
1235 |
|
|
this.key = key; |
1236 |
|
|
this.value = value; |
1237 |
|
|
} |
1238 |
|
|
|
1239 |
|
|
public SimpleEntry(Entry<K,V> e) { |
1240 |
|
|
this.key = e.getKey(); |
1241 |
|
|
this.value = e.getValue(); |
1242 |
|
|
} |
1243 |
|
|
|
1244 |
|
|
public K getKey() { |
1245 |
|
|
return key; |
1246 |
|
|
} |
1247 |
|
|
|
1248 |
|
|
public V getValue() { |
1249 |
|
|
return value; |
1250 |
|
|
} |
1251 |
|
|
|
1252 |
|
|
public V setValue(V value) { |
1253 |
|
|
V oldValue = this.value; |
1254 |
|
|
this.value = value; |
1255 |
|
|
return oldValue; |
1256 |
|
|
} |
1257 |
|
|
|
1258 |
|
|
public boolean equals(Object o) { |
1259 |
|
|
if (!(o instanceof Map.Entry)) |
1260 |
|
|
return false; |
1261 |
|
|
Map.Entry e = (Map.Entry)o; |
1262 |
|
|
return eq(key, e.getKey()) && eq(value, e.getValue()); |
1263 |
|
|
} |
1264 |
|
|
|
1265 |
|
|
public int hashCode() { |
1266 |
|
|
return ((key == null) ? 0 : key.hashCode()) ^ |
1267 |
|
|
((value == null) ? 0 : value.hashCode()); |
1268 |
|
|
} |
1269 |
|
|
|
1270 |
|
|
public String toString() { |
1271 |
|
|
return key + "=" + value; |
1272 |
|
|
} |
1273 |
|
|
|
1274 |
|
|
private static boolean eq(Object o1, Object o2) { |
1275 |
|
|
return (o1 == null ? o2 == null : o1.equals(o2)); |
1276 |
dl |
1.4 |
} |
1277 |
tim |
1.1 |
} |
1278 |
|
|
|
1279 |
dl |
1.4 |
/* ---------------- Serialization Support -------------- */ |
1280 |
|
|
|
1281 |
tim |
1.1 |
/** |
1282 |
|
|
* Save the state of the <tt>ConcurrentHashMap</tt> |
1283 |
|
|
* instance to a stream (i.e., |
1284 |
|
|
* serialize it). |
1285 |
dl |
1.8 |
* @param s the stream |
1286 |
tim |
1.1 |
* @serialData |
1287 |
|
|
* the key (Object) and value (Object) |
1288 |
|
|
* for each key-value mapping, followed by a null pair. |
1289 |
|
|
* The key-value mappings are emitted in no particular order. |
1290 |
|
|
*/ |
1291 |
|
|
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
1292 |
|
|
s.defaultWriteObject(); |
1293 |
|
|
|
1294 |
|
|
for (int k = 0; k < segments.length; ++k) { |
1295 |
tim |
1.12 |
Segment<K,V> seg = (Segment<K,V>)segments[k]; |
1296 |
dl |
1.2 |
seg.lock(); |
1297 |
|
|
try { |
1298 |
tim |
1.11 |
HashEntry[] tab = seg.table; |
1299 |
dl |
1.4 |
for (int i = 0; i < tab.length; ++i) { |
1300 |
tim |
1.12 |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; e != null; e = e.next) { |
1301 |
dl |
1.4 |
s.writeObject(e.key); |
1302 |
|
|
s.writeObject(e.value); |
1303 |
|
|
} |
1304 |
|
|
} |
1305 |
tim |
1.16 |
} finally { |
1306 |
dl |
1.2 |
seg.unlock(); |
1307 |
|
|
} |
1308 |
tim |
1.1 |
} |
1309 |
|
|
s.writeObject(null); |
1310 |
|
|
s.writeObject(null); |
1311 |
|
|
} |
1312 |
|
|
|
1313 |
|
|
/** |
1314 |
|
|
* Reconstitute the <tt>ConcurrentHashMap</tt> |
1315 |
|
|
* instance from a stream (i.e., |
1316 |
|
|
* deserialize it). |
1317 |
dl |
1.8 |
* @param s the stream |
1318 |
tim |
1.1 |
*/ |
1319 |
|
|
private void readObject(java.io.ObjectInputStream s) |
1320 |
|
|
throws IOException, ClassNotFoundException { |
1321 |
|
|
s.defaultReadObject(); |
1322 |
|
|
|
1323 |
dl |
1.4 |
// Initialize each segment to be minimally sized, and let grow. |
1324 |
|
|
for (int i = 0; i < segments.length; ++i) { |
1325 |
tim |
1.11 |
segments[i].setTable(new HashEntry[1]); |
1326 |
dl |
1.4 |
} |
1327 |
tim |
1.1 |
|
1328 |
|
|
// Read the keys and values, and put the mappings in the table |
1329 |
dl |
1.9 |
for (;;) { |
1330 |
tim |
1.1 |
K key = (K) s.readObject(); |
1331 |
|
|
V value = (V) s.readObject(); |
1332 |
|
|
if (key == null) |
1333 |
|
|
break; |
1334 |
|
|
put(key, value); |
1335 |
|
|
} |
1336 |
|
|
} |
1337 |
|
|
} |
1338 |
tim |
1.11 |
|