1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
39 |
|
* |
40 |
|
* <p> The allowed concurrency among update operations is guided by |
41 |
|
* the optional <tt>concurrencyLevel</tt> constructor argument |
42 |
< |
* (default 16), which is used as a hint for internal sizing. The |
42 |
> |
* (default <tt>16</tt>), 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 |
49 |
|
* and a significantly lower value can lead to thread contention. But |
50 |
|
* overestimates and underestimates within an order of magnitude do |
51 |
|
* 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. |
52 |
> |
* appropriate when it is known that only one thread will modify and |
53 |
> |
* all others will only read. Also, resizing this or any other kind of |
54 |
> |
* hash table is a relatively slow operation, so, when possible, it is |
55 |
> |
* a good idea to provide estimates of expected table sizes in |
56 |
> |
* constructors. |
57 |
|
* |
58 |
< |
* <p>This class implements all of the <em>optional</em> methods |
59 |
< |
* of the {@link Map} and {@link Iterator} interfaces. |
58 |
> |
* <p>This class and its views and iterators implement all of the |
59 |
> |
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
60 |
> |
* interfaces. |
61 |
|
* |
62 |
|
* <p> Like {@link java.util.Hashtable} but unlike {@link |
63 |
|
* java.util.HashMap}, this class does NOT allow <tt>null</tt> to be |
64 |
|
* used as a key or value. |
65 |
|
* |
66 |
+ |
* <p>This class is a member of the |
67 |
+ |
* <a href="{@docRoot}/../guide/collections/index.html"> |
68 |
+ |
* Java Collections Framework</a>. |
69 |
+ |
* |
70 |
|
* @since 1.5 |
71 |
|
* @author Doug Lea |
72 |
|
* @param <K> the type of keys maintained by this map |
73 |
< |
* @param <V> the type of mapped values |
73 |
> |
* @param <V> the type of mapped values |
74 |
|
*/ |
75 |
|
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> |
76 |
< |
implements ConcurrentMap<K, V>, Cloneable, Serializable { |
76 |
> |
implements ConcurrentMap<K, V>, Serializable { |
77 |
|
private static final long serialVersionUID = 7249069246763182397L; |
78 |
|
|
79 |
|
/* |
84 |
|
/* ---------------- Constants -------------- */ |
85 |
|
|
86 |
|
/** |
87 |
< |
* The default initial number of table slots for this table. |
88 |
< |
* Used when not otherwise specified in constructor. |
87 |
> |
* The default initial capacity for this table, |
88 |
> |
* used when not otherwise specified in a constructor. |
89 |
> |
*/ |
90 |
> |
static final int DEFAULT_INITIAL_CAPACITY = 16; |
91 |
> |
|
92 |
> |
/** |
93 |
> |
* The default load factor for this table, used when not |
94 |
> |
* otherwise specified in a constructor. |
95 |
> |
*/ |
96 |
> |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
97 |
> |
|
98 |
> |
/** |
99 |
> |
* The default concurrency level for this table, used when not |
100 |
> |
* otherwise specified in a constructor. |
101 |
|
*/ |
102 |
< |
private static int DEFAULT_INITIAL_CAPACITY = 16; |
102 |
> |
static final int DEFAULT_CONCURRENCY_LEVEL = 16; |
103 |
|
|
104 |
|
/** |
105 |
|
* The maximum capacity, used if a higher value is implicitly |
107 |
|
* be a power of two <= 1<<30 to ensure that entries are indexible |
108 |
|
* using ints. |
109 |
|
*/ |
110 |
< |
static final int MAXIMUM_CAPACITY = 1 << 30; |
110 |
> |
static final int MAXIMUM_CAPACITY = 1 << 30; |
111 |
|
|
112 |
|
/** |
113 |
< |
* The default load factor for this table. Used when not |
114 |
< |
* otherwise specified in constructor. |
113 |
> |
* The maximum number of segments to allow; used to bound |
114 |
> |
* constructor arguments. |
115 |
|
*/ |
116 |
< |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
116 |
> |
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative |
117 |
|
|
118 |
|
/** |
119 |
< |
* The default number of concurrency control segments. |
120 |
< |
**/ |
121 |
< |
private static final int DEFAULT_SEGMENTS = 16; |
122 |
< |
|
103 |
< |
/** |
104 |
< |
* The maximum number of segments to allow; used to bound ctor arguments. |
119 |
> |
* Number of unsynchronized retries in size and containsValue |
120 |
> |
* methods before resorting to locking. This is used to avoid |
121 |
> |
* unbounded retries if tables undergo continuous modification |
122 |
> |
* which would make it impossible to obtain an accurate result. |
123 |
|
*/ |
124 |
< |
private static final int MAX_SEGMENTS = 1 << 16; // slightly conservative |
124 |
> |
static final int RETRIES_BEFORE_LOCK = 2; |
125 |
|
|
126 |
|
/* ---------------- Fields -------------- */ |
127 |
|
|
128 |
|
/** |
129 |
|
* Mask value for indexing into segments. The upper bits of a |
130 |
|
* key's hash code are used to choose the segment. |
131 |
< |
**/ |
132 |
< |
private final int segmentMask; |
131 |
> |
*/ |
132 |
> |
final int segmentMask; |
133 |
|
|
134 |
|
/** |
135 |
|
* Shift value for indexing within segments. |
136 |
< |
**/ |
137 |
< |
private final int segmentShift; |
136 |
> |
*/ |
137 |
> |
final int segmentShift; |
138 |
|
|
139 |
|
/** |
140 |
|
* The segments, each of which is a specialized hash table |
141 |
|
*/ |
142 |
< |
private final Segment[] segments; |
142 |
> |
final Segment[] segments; |
143 |
|
|
144 |
< |
private transient Set<K> keySet; |
145 |
< |
private transient Set<Map.Entry<K,V>> entrySet; |
146 |
< |
private transient Collection<V> values; |
144 |
> |
transient Set<K> keySet; |
145 |
> |
transient Set<Map.Entry<K,V>> entrySet; |
146 |
> |
transient Collection<V> values; |
147 |
|
|
148 |
|
/* ---------------- Small Utilities -------------- */ |
149 |
|
|
150 |
|
/** |
151 |
< |
* Return a hash code for non-null Object x. |
152 |
< |
* Uses the same hash code spreader as most other j.u hash tables. |
151 |
> |
* Returns a hash code for non-null Object x. |
152 |
> |
* Uses the same hash code spreader as most other java.util hash tables. |
153 |
|
* @param x the object serving as a key |
154 |
|
* @return the hash code |
155 |
|
*/ |
156 |
< |
private static int hash(Object x) { |
156 |
> |
static int hash(Object x) { |
157 |
|
int h = x.hashCode(); |
158 |
|
h += ~(h << 9); |
159 |
|
h ^= (h >>> 14); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
< |
* Return the segment that should be used for key with given hash |
166 |
> |
* Returns the segment that should be used for key with given hash |
167 |
> |
* @param hash the hash code for the key |
168 |
> |
* @return the segment |
169 |
|
*/ |
170 |
< |
private Segment<K,V> segmentFor(int hash) { |
170 |
> |
final Segment<K,V> segmentFor(int hash) { |
171 |
|
return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask]; |
172 |
|
} |
173 |
|
|
174 |
|
/* ---------------- Inner Classes -------------- */ |
175 |
|
|
176 |
|
/** |
177 |
+ |
* ConcurrentHashMap list entry. Note that this is never exported |
178 |
+ |
* out as a user-visible Map.Entry. |
179 |
+ |
* |
180 |
+ |
* Because the value field is volatile, not final, it is legal wrt |
181 |
+ |
* the Java Memory Model for an unsynchronized reader to see null |
182 |
+ |
* instead of initial value when read via a data race. Although a |
183 |
+ |
* reordering leading to this is not likely to ever actually |
184 |
+ |
* occur, the Segment.readValueUnderLock method is used as a |
185 |
+ |
* backup in case a null (pre-initialized) value is ever seen in |
186 |
+ |
* an unsynchronized access method. |
187 |
+ |
*/ |
188 |
+ |
static final class HashEntry<K,V> { |
189 |
+ |
final K key; |
190 |
+ |
final int hash; |
191 |
+ |
volatile V value; |
192 |
+ |
final HashEntry<K,V> next; |
193 |
+ |
|
194 |
+ |
HashEntry(K key, int hash, HashEntry<K,V> next, V value) { |
195 |
+ |
this.key = key; |
196 |
+ |
this.hash = hash; |
197 |
+ |
this.next = next; |
198 |
+ |
this.value = value; |
199 |
+ |
} |
200 |
+ |
} |
201 |
+ |
|
202 |
+ |
/** |
203 |
|
* Segments are specialized versions of hash tables. This |
204 |
|
* subclasses from ReentrantLock opportunistically, just to |
205 |
|
* simplify some locking and avoid separate construction. |
206 |
< |
**/ |
207 |
< |
private static final class Segment<K,V> extends ReentrantLock implements Serializable { |
206 |
> |
*/ |
207 |
> |
static final class Segment<K,V> extends ReentrantLock implements Serializable { |
208 |
|
/* |
209 |
|
* Segments maintain a table of entry lists that are ALWAYS |
210 |
|
* kept in a consistent state, so can be read without locking. |
217 |
|
* is less than two for the default load factor threshold.) |
218 |
|
* |
219 |
|
* Read operations can thus proceed without locking, but rely |
220 |
< |
* on a memory barrier to ensure that completed write |
221 |
< |
* operations performed by other threads are |
222 |
< |
* noticed. Conveniently, the "count" field, tracking the |
223 |
< |
* number of elements, can also serve as the volatile variable |
224 |
< |
* providing proper read/write barriers. This is convenient |
225 |
< |
* because this field needs to be read in many read operations |
180 |
< |
* anyway. |
181 |
< |
* |
182 |
< |
* Implementors note. The basic rules for all this are: |
220 |
> |
* on selected uses of volatiles to ensure that completed |
221 |
> |
* write operations performed by other threads are |
222 |
> |
* noticed. For most purposes, the "count" field, tracking the |
223 |
> |
* number of elements, serves as that volatile variable |
224 |
> |
* ensuring visibility. This is convenient because this field |
225 |
> |
* needs to be read in many read operations anyway: |
226 |
|
* |
227 |
< |
* - All unsynchronized read operations must first read the |
227 |
> |
* - All (unsynchronized) read operations must first read the |
228 |
|
* "count" field, and should not look at table entries if |
229 |
|
* it is 0. |
230 |
|
* |
231 |
< |
* - All synchronized write operations should write to |
232 |
< |
* the "count" field after updating. The operations must not |
233 |
< |
* take any action that could even momentarily cause |
234 |
< |
* a concurrent read operation to see inconsistent |
235 |
< |
* data. This is made easier by the nature of the read |
236 |
< |
* operations in Map. For example, no operation |
231 |
> |
* - All (synchronized) write operations should write to |
232 |
> |
* the "count" field after structurally changing any bin. |
233 |
> |
* The operations must not take any action that could even |
234 |
> |
* momentarily cause a concurrent read operation to see |
235 |
> |
* inconsistent data. This is made easier by the nature of |
236 |
> |
* the read operations in Map. For example, no operation |
237 |
|
* can reveal that the table has grown but the threshold |
238 |
|
* has not yet been updated, so there are no atomicity |
239 |
|
* requirements for this with respect to reads. |
240 |
|
* |
241 |
< |
* As a guide, all critical volatile reads and writes are marked |
242 |
< |
* in code comments. |
241 |
> |
* As a guide, all critical volatile reads and writes to the |
242 |
> |
* count field are marked in code comments. |
243 |
|
*/ |
244 |
|
|
245 |
|
private static final long serialVersionUID = 2249069246763182397L; |
246 |
|
|
247 |
|
/** |
248 |
|
* The number of elements in this segment's region. |
249 |
< |
**/ |
249 |
> |
*/ |
250 |
|
transient volatile int count; |
251 |
|
|
252 |
|
/** |
253 |
< |
* Number of updates; used for checking lack of modifications |
254 |
< |
* in bulk-read methods. |
253 |
> |
* Number of updates that alter the size of the table. This is |
254 |
> |
* used during bulk-read methods to make sure they see a |
255 |
> |
* consistent snapshot: If modCounts change during a traversal |
256 |
> |
* of segments computing size or checking containsValue, then |
257 |
> |
* we might have an inconsistent view of state so (usually) |
258 |
> |
* must retry. |
259 |
|
*/ |
260 |
|
transient int modCount; |
261 |
|
|
264 |
|
* (The value of this field is always (int)(capacity * |
265 |
|
* loadFactor).) |
266 |
|
*/ |
267 |
< |
private transient int threshold; |
267 |
> |
transient int threshold; |
268 |
|
|
269 |
|
/** |
270 |
< |
* The per-segment table |
270 |
> |
* The per-segment table. Declared as a raw type, casted |
271 |
> |
* to HashEntry<K,V> on each use. |
272 |
|
*/ |
273 |
< |
transient HashEntry[] table; |
273 |
> |
transient volatile HashEntry[] table; |
274 |
|
|
275 |
|
/** |
276 |
|
* The load factor for the hash table. Even though this value |
278 |
|
* links to outer object. |
279 |
|
* @serial |
280 |
|
*/ |
281 |
< |
private final float loadFactor; |
281 |
> |
final float loadFactor; |
282 |
|
|
283 |
|
Segment(int initialCapacity, float lf) { |
284 |
|
loadFactor = lf; |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* Set table to new HashEntry array. |
289 |
> |
* Sets table to new HashEntry array. |
290 |
|
* Call only while holding lock or in constructor. |
291 |
< |
**/ |
292 |
< |
private void setTable(HashEntry[] newTable) { |
245 |
< |
table = newTable; |
291 |
> |
*/ |
292 |
> |
void setTable(HashEntry[] newTable) { |
293 |
|
threshold = (int)(newTable.length * loadFactor); |
294 |
< |
count = count; // write-volatile |
294 |
> |
table = newTable; |
295 |
> |
} |
296 |
> |
|
297 |
> |
/** |
298 |
> |
* Returns properly casted first entry of bin for given hash. |
299 |
> |
*/ |
300 |
> |
HashEntry<K,V> getFirst(int hash) { |
301 |
> |
HashEntry[] tab = table; |
302 |
> |
return (HashEntry<K,V>) tab[hash & (tab.length - 1)]; |
303 |
> |
} |
304 |
> |
|
305 |
> |
/** |
306 |
> |
* Reads value field of an entry under lock. Called if value |
307 |
> |
* field ever appears to be null. This is possible only if a |
308 |
> |
* compiler happens to reorder a HashEntry initialization with |
309 |
> |
* its table assignment, which is legal under memory model |
310 |
> |
* but is not known to ever occur. |
311 |
> |
*/ |
312 |
> |
V readValueUnderLock(HashEntry<K,V> e) { |
313 |
> |
lock(); |
314 |
> |
try { |
315 |
> |
return e.value; |
316 |
> |
} finally { |
317 |
> |
unlock(); |
318 |
> |
} |
319 |
|
} |
320 |
|
|
321 |
|
/* Specialized implementations of map methods */ |
322 |
|
|
323 |
|
V get(Object key, int hash) { |
324 |
|
if (count != 0) { // read-volatile |
325 |
< |
HashEntry[] tab = table; |
255 |
< |
int index = hash & (tab.length - 1); |
256 |
< |
HashEntry<K,V> e = (HashEntry<K,V>) tab[index]; |
325 |
> |
HashEntry<K,V> e = getFirst(hash); |
326 |
|
while (e != null) { |
327 |
< |
if (e.hash == hash && key.equals(e.key)) |
328 |
< |
return e.value; |
327 |
> |
if (e.hash == hash && key.equals(e.key)) { |
328 |
> |
V v = e.value; |
329 |
> |
if (v != null) |
330 |
> |
return v; |
331 |
> |
return readValueUnderLock(e); // recheck |
332 |
> |
} |
333 |
|
e = e.next; |
334 |
|
} |
335 |
|
} |
338 |
|
|
339 |
|
boolean containsKey(Object key, int hash) { |
340 |
|
if (count != 0) { // read-volatile |
341 |
< |
HashEntry[] tab = table; |
269 |
< |
int index = hash & (tab.length - 1); |
270 |
< |
HashEntry<K,V> e = (HashEntry<K,V>) tab[index]; |
341 |
> |
HashEntry<K,V> e = getFirst(hash); |
342 |
|
while (e != null) { |
343 |
|
if (e.hash == hash && key.equals(e.key)) |
344 |
|
return true; |
352 |
|
if (count != 0) { // read-volatile |
353 |
|
HashEntry[] tab = table; |
354 |
|
int len = tab.length; |
355 |
< |
for (int i = 0 ; i < len; i++) |
356 |
< |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i] ; e != null ; e = e.next) |
357 |
< |
if (value.equals(e.value)) |
355 |
> |
for (int i = 0 ; i < len; i++) { |
356 |
> |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; |
357 |
> |
e != null ; |
358 |
> |
e = e.next) { |
359 |
> |
V v = e.value; |
360 |
> |
if (v == null) // recheck |
361 |
> |
v = readValueUnderLock(e); |
362 |
> |
if (value.equals(v)) |
363 |
|
return true; |
364 |
+ |
} |
365 |
+ |
} |
366 |
|
} |
367 |
|
return false; |
368 |
|
} |
370 |
|
boolean replace(K key, int hash, V oldValue, V newValue) { |
371 |
|
lock(); |
372 |
|
try { |
373 |
< |
int c = count; |
374 |
< |
HashEntry[] tab = table; |
297 |
< |
int index = hash & (tab.length - 1); |
298 |
< |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
299 |
< |
HashEntry<K,V> e = first; |
300 |
< |
for (;;) { |
301 |
< |
if (e == null) |
302 |
< |
return false; |
303 |
< |
if (e.hash == hash && key.equals(e.key)) |
304 |
< |
break; |
373 |
> |
HashEntry<K,V> e = getFirst(hash); |
374 |
> |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
375 |
|
e = e.next; |
306 |
– |
} |
307 |
– |
|
308 |
– |
V v = e.value; |
309 |
– |
if (v == null || !oldValue.equals(v)) |
310 |
– |
return false; |
376 |
|
|
377 |
< |
e.value = newValue; |
378 |
< |
count = c; // write-volatile |
379 |
< |
return true; |
380 |
< |
|
377 |
> |
boolean replaced = false; |
378 |
> |
if (e != null && oldValue.equals(e.value)) { |
379 |
> |
replaced = true; |
380 |
> |
e.value = newValue; |
381 |
> |
} |
382 |
> |
return replaced; |
383 |
|
} finally { |
384 |
|
unlock(); |
385 |
|
} |
388 |
|
V replace(K key, int hash, V newValue) { |
389 |
|
lock(); |
390 |
|
try { |
391 |
< |
int c = count; |
392 |
< |
HashEntry[] tab = table; |
326 |
< |
int index = hash & (tab.length - 1); |
327 |
< |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
328 |
< |
HashEntry<K,V> e = first; |
329 |
< |
for (;;) { |
330 |
< |
if (e == null) |
331 |
< |
return null; |
332 |
< |
if (e.hash == hash && key.equals(e.key)) |
333 |
< |
break; |
391 |
> |
HashEntry<K,V> e = getFirst(hash); |
392 |
> |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
393 |
|
e = e.next; |
335 |
– |
} |
394 |
|
|
395 |
< |
V v = e.value; |
396 |
< |
e.value = newValue; |
397 |
< |
count = c; // write-volatile |
398 |
< |
return v; |
399 |
< |
|
395 |
> |
V oldValue = null; |
396 |
> |
if (e != null) { |
397 |
> |
oldValue = e.value; |
398 |
> |
e.value = newValue; |
399 |
> |
} |
400 |
> |
return oldValue; |
401 |
|
} finally { |
402 |
|
unlock(); |
403 |
|
} |
408 |
|
lock(); |
409 |
|
try { |
410 |
|
int c = count; |
411 |
+ |
if (c++ > threshold) // ensure capacity |
412 |
+ |
rehash(); |
413 |
|
HashEntry[] tab = table; |
414 |
|
int index = hash & (tab.length - 1); |
415 |
|
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
416 |
+ |
HashEntry<K,V> e = first; |
417 |
+ |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
418 |
+ |
e = e.next; |
419 |
|
|
420 |
< |
for (HashEntry<K,V> e = first; e != null; e = (HashEntry<K,V>) e.next) { |
421 |
< |
if (e.hash == hash && key.equals(e.key)) { |
422 |
< |
V oldValue = e.value; |
423 |
< |
if (!onlyIfAbsent) |
424 |
< |
e.value = value; |
361 |
< |
++modCount; |
362 |
< |
count = c; // write-volatile |
363 |
< |
return oldValue; |
364 |
< |
} |
420 |
> |
V oldValue; |
421 |
> |
if (e != null) { |
422 |
> |
oldValue = e.value; |
423 |
> |
if (!onlyIfAbsent) |
424 |
> |
e.value = value; |
425 |
|
} |
426 |
< |
|
427 |
< |
tab[index] = new HashEntry<K,V>(hash, key, value, first); |
428 |
< |
++modCount; |
429 |
< |
++c; |
430 |
< |
count = c; // write-volatile |
431 |
< |
if (c > threshold) |
432 |
< |
setTable(rehash(tab)); |
373 |
< |
return null; |
426 |
> |
else { |
427 |
> |
oldValue = null; |
428 |
> |
++modCount; |
429 |
> |
tab[index] = new HashEntry<K,V>(key, hash, first, value); |
430 |
> |
count = c; // write-volatile |
431 |
> |
} |
432 |
> |
return oldValue; |
433 |
|
} finally { |
434 |
|
unlock(); |
435 |
|
} |
436 |
|
} |
437 |
|
|
438 |
< |
private HashEntry[] rehash(HashEntry[] oldTable) { |
438 |
> |
void rehash() { |
439 |
> |
HashEntry[] oldTable = table; |
440 |
|
int oldCapacity = oldTable.length; |
441 |
|
if (oldCapacity >= MAXIMUM_CAPACITY) |
442 |
< |
return oldTable; |
442 |
> |
return; |
443 |
|
|
444 |
|
/* |
445 |
|
* Reclassify nodes in each list to new Map. Because we are |
456 |
|
*/ |
457 |
|
|
458 |
|
HashEntry[] newTable = new HashEntry[oldCapacity << 1]; |
459 |
+ |
threshold = (int)(newTable.length * loadFactor); |
460 |
|
int sizeMask = newTable.length - 1; |
461 |
|
for (int i = 0; i < oldCapacity ; i++) { |
462 |
|
// We need to guarantee that any existing reads of old Map can |
489 |
|
// Clone all remaining nodes |
490 |
|
for (HashEntry<K,V> p = e; p != lastRun; p = p.next) { |
491 |
|
int k = p.hash & sizeMask; |
492 |
< |
newTable[k] = new HashEntry<K,V>(p.hash, |
493 |
< |
p.key, |
494 |
< |
p.value, |
434 |
< |
(HashEntry<K,V>) newTable[k]); |
492 |
> |
HashEntry<K,V> n = (HashEntry<K,V>)newTable[k]; |
493 |
> |
newTable[k] = new HashEntry<K,V>(p.key, p.hash, |
494 |
> |
n, p.value); |
495 |
|
} |
496 |
|
} |
497 |
|
} |
498 |
|
} |
499 |
< |
return newTable; |
499 |
> |
table = newTable; |
500 |
|
} |
501 |
|
|
502 |
|
/** |
505 |
|
V remove(Object key, int hash, Object value) { |
506 |
|
lock(); |
507 |
|
try { |
508 |
< |
int c = count; |
508 |
> |
int c = count - 1; |
509 |
|
HashEntry[] tab = table; |
510 |
|
int index = hash & (tab.length - 1); |
511 |
|
HashEntry<K,V> first = (HashEntry<K,V>)tab[index]; |
452 |
– |
|
512 |
|
HashEntry<K,V> e = first; |
513 |
< |
for (;;) { |
455 |
< |
if (e == null) |
456 |
< |
return null; |
457 |
< |
if (e.hash == hash && key.equals(e.key)) |
458 |
< |
break; |
513 |
> |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
514 |
|
e = e.next; |
460 |
– |
} |
515 |
|
|
516 |
< |
V oldValue = e.value; |
517 |
< |
if (value != null && !value.equals(oldValue)) |
518 |
< |
return null; |
519 |
< |
|
520 |
< |
// All entries following removed node can stay in list, but |
521 |
< |
// all preceding ones need to be cloned. |
522 |
< |
HashEntry<K,V> newFirst = e.next; |
523 |
< |
for (HashEntry<K,V> p = first; p != e; p = p.next) |
524 |
< |
newFirst = new HashEntry<K,V>(p.hash, p.key, |
525 |
< |
p.value, newFirst); |
526 |
< |
tab[index] = newFirst; |
527 |
< |
++modCount; |
528 |
< |
count = c-1; // write-volatile |
516 |
> |
V oldValue = null; |
517 |
> |
if (e != null) { |
518 |
> |
V v = e.value; |
519 |
> |
if (value == null || value.equals(v)) { |
520 |
> |
oldValue = v; |
521 |
> |
// All entries following removed node can stay |
522 |
> |
// in list, but all preceding ones need to be |
523 |
> |
// cloned. |
524 |
> |
++modCount; |
525 |
> |
HashEntry<K,V> newFirst = e.next; |
526 |
> |
for (HashEntry<K,V> p = first; p != e; p = p.next) |
527 |
> |
newFirst = new HashEntry<K,V>(p.key, p.hash, |
528 |
> |
newFirst, p.value); |
529 |
> |
tab[index] = newFirst; |
530 |
> |
count = c; // write-volatile |
531 |
> |
} |
532 |
> |
} |
533 |
|
return oldValue; |
534 |
|
} finally { |
535 |
|
unlock(); |
537 |
|
} |
538 |
|
|
539 |
|
void clear() { |
540 |
< |
lock(); |
541 |
< |
try { |
542 |
< |
HashEntry[] tab = table; |
543 |
< |
for (int i = 0; i < tab.length ; i++) |
544 |
< |
tab[i] = null; |
545 |
< |
++modCount; |
546 |
< |
count = 0; // write-volatile |
547 |
< |
} finally { |
548 |
< |
unlock(); |
540 |
> |
if (count != 0) { |
541 |
> |
lock(); |
542 |
> |
try { |
543 |
> |
HashEntry[] tab = table; |
544 |
> |
for (int i = 0; i < tab.length ; i++) |
545 |
> |
tab[i] = null; |
546 |
> |
++modCount; |
547 |
> |
count = 0; // write-volatile |
548 |
> |
} finally { |
549 |
> |
unlock(); |
550 |
> |
} |
551 |
|
} |
552 |
|
} |
553 |
|
} |
554 |
|
|
495 |
– |
/** |
496 |
– |
* ConcurrentHashMap list entry. Note that this is never exported |
497 |
– |
* out as a user-visible Map.Entry |
498 |
– |
*/ |
499 |
– |
private static class HashEntry<K,V> { |
500 |
– |
private final K key; |
501 |
– |
private V value; |
502 |
– |
private final int hash; |
503 |
– |
private final HashEntry<K,V> next; |
504 |
– |
|
505 |
– |
HashEntry(int hash, K key, V value, HashEntry<K,V> next) { |
506 |
– |
this.value = value; |
507 |
– |
this.hash = hash; |
508 |
– |
this.key = key; |
509 |
– |
this.next = next; |
510 |
– |
} |
511 |
– |
} |
555 |
|
|
556 |
|
|
557 |
|
/* ---------------- Public operations -------------- */ |
558 |
|
|
559 |
|
/** |
560 |
< |
* Constructs a new, empty map with the specified initial |
561 |
< |
* capacity and the specified load factor. |
560 |
> |
* Creates a new, empty map with the specified initial |
561 |
> |
* capacity, load factor and concurrency level. |
562 |
|
* |
563 |
|
* @param initialCapacity the initial capacity. The implementation |
564 |
|
* performs internal sizing to accommodate this many elements. |
565 |
|
* @param loadFactor the load factor threshold, used to control resizing. |
566 |
+ |
* Resizing may be performed when the average number of elements per |
567 |
+ |
* bin exceeds this threshold. |
568 |
|
* @param concurrencyLevel the estimated number of concurrently |
569 |
|
* updating threads. The implementation performs internal sizing |
570 |
< |
* to try to accommodate this many threads. |
570 |
> |
* to try to accommodate this many threads. |
571 |
|
* @throws IllegalArgumentException if the initial capacity is |
572 |
|
* negative or the load factor or concurrencyLevel are |
573 |
|
* nonpositive. |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* Constructs a new, empty map with the specified initial |
609 |
< |
* capacity, and with default load factor and concurrencyLevel. |
608 |
> |
* Creates a new, empty map with the specified initial capacity |
609 |
> |
* and load factor and with the default concurrencyLevel |
610 |
> |
* (<tt>16</tt>). |
611 |
|
* |
612 |
|
* @param initialCapacity The implementation performs internal |
613 |
|
* sizing to accommodate this many elements. |
614 |
+ |
* @param loadFactor the load factor threshold, used to control resizing. |
615 |
+ |
* @throws IllegalArgumentException if the initial capacity of |
616 |
+ |
* elements is negative or the load factor is nonpositive |
617 |
+ |
*/ |
618 |
+ |
public ConcurrentHashMap(int initialCapacity, float loadFactor) { |
619 |
+ |
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL); |
620 |
+ |
} |
621 |
+ |
|
622 |
+ |
/** |
623 |
+ |
* Creates a new, empty map with the specified initial capacity, |
624 |
+ |
* and with default load factor (<tt>0.75f</tt>) |
625 |
+ |
* and concurrencyLevel (<tt>16</tt>). |
626 |
+ |
* |
627 |
+ |
* @param initialCapacity the initial capacity. The implementation |
628 |
+ |
* performs internal sizing to accommodate this many elements. |
629 |
|
* @throws IllegalArgumentException if the initial capacity of |
630 |
|
* elements is negative. |
631 |
|
*/ |
632 |
|
public ConcurrentHashMap(int initialCapacity) { |
633 |
< |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
633 |
> |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
634 |
|
} |
635 |
|
|
636 |
|
/** |
637 |
< |
* Constructs a new, empty map with a default initial capacity, |
638 |
< |
* load factor, and concurrencyLevel. |
637 |
> |
* Creates a new, empty map with a default initial capacity |
638 |
> |
* (<tt>16</tt>), load factor |
639 |
> |
* (<tt>0.75f</tt>), and concurrencyLevel |
640 |
> |
* (<tt>16</tt>). |
641 |
|
*/ |
642 |
|
public ConcurrentHashMap() { |
643 |
< |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
643 |
> |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* Constructs a new map with the same mappings as the given map. The |
648 |
< |
* map is created with a capacity of twice the number of mappings in |
649 |
< |
* the given map or 11 (whichever is greater), and a default load factor. |
647 |
> |
* Creates a new map with the same mappings as the given map. The |
648 |
> |
* map is created with a capacity of 1.5 times the number of |
649 |
> |
* mappings in the given map or <tt>16</tt> |
650 |
> |
* (whichever is greater), and a default load factor |
651 |
> |
* (<tt>0.75f</tt>) and concurrencyLevel |
652 |
> |
* (<tt>16</tt>). |
653 |
> |
* @param t the map |
654 |
|
*/ |
655 |
< |
public <A extends K, B extends V> ConcurrentHashMap(Map<A,B> t) { |
655 |
> |
public ConcurrentHashMap(Map<? extends K, ? extends V> t) { |
656 |
|
this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1, |
657 |
< |
11), |
658 |
< |
DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
657 |
> |
DEFAULT_INITIAL_CAPACITY), |
658 |
> |
DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
659 |
|
putAll(t); |
660 |
|
} |
661 |
|
|
662 |
< |
// inherit Map javadoc |
662 |
> |
/** |
663 |
> |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
664 |
> |
* |
665 |
> |
* @return <tt>true</tt> if this map contains no key-value mappings. |
666 |
> |
*/ |
667 |
|
public boolean isEmpty() { |
668 |
+ |
final Segment[] segments = this.segments; |
669 |
|
/* |
670 |
< |
* We need to keep track of per-segment modCounts to avoid ABA |
670 |
> |
* We keep track of per-segment modCounts to avoid ABA |
671 |
|
* problems in which an element in one segment was added and |
672 |
|
* in another removed during traversal, in which case the |
673 |
|
* table was never actually empty at any point. Note the |
680 |
|
for (int i = 0; i < segments.length; ++i) { |
681 |
|
if (segments[i].count != 0) |
682 |
|
return false; |
683 |
< |
else |
683 |
> |
else |
684 |
|
mcsum += mc[i] = segments[i].modCount; |
685 |
|
} |
686 |
|
// If mcsum happens to be zero, then we know we got a snapshot |
689 |
|
if (mcsum != 0) { |
690 |
|
for (int i = 0; i < segments.length; ++i) { |
691 |
|
if (segments[i].count != 0 || |
692 |
< |
mc[i] != segments[i].modCount) |
692 |
> |
mc[i] != segments[i].modCount) |
693 |
|
return false; |
694 |
|
} |
695 |
|
} |
696 |
|
return true; |
697 |
|
} |
698 |
|
|
699 |
< |
// inherit Map javadoc |
699 |
> |
/** |
700 |
> |
* Returns the number of key-value mappings in this map. If the |
701 |
> |
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
702 |
> |
* <tt>Integer.MAX_VALUE</tt>. |
703 |
> |
* |
704 |
> |
* @return the number of key-value mappings in this map. |
705 |
> |
*/ |
706 |
|
public int size() { |
707 |
+ |
final Segment[] segments = this.segments; |
708 |
+ |
long sum = 0; |
709 |
+ |
long check = 0; |
710 |
|
int[] mc = new int[segments.length]; |
711 |
< |
for (;;) { |
712 |
< |
long sum = 0; |
711 |
> |
// Try a few times to get accurate count. On failure due to |
712 |
> |
// continuous async changes in table, resort to locking. |
713 |
> |
for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) { |
714 |
> |
check = 0; |
715 |
> |
sum = 0; |
716 |
|
int mcsum = 0; |
717 |
|
for (int i = 0; i < segments.length; ++i) { |
718 |
|
sum += segments[i].count; |
719 |
|
mcsum += mc[i] = segments[i].modCount; |
720 |
|
} |
637 |
– |
int check = 0; |
721 |
|
if (mcsum != 0) { |
722 |
|
for (int i = 0; i < segments.length; ++i) { |
723 |
|
check += segments[i].count; |
727 |
|
} |
728 |
|
} |
729 |
|
} |
730 |
< |
if (check == sum) { |
731 |
< |
if (sum > Integer.MAX_VALUE) |
649 |
< |
return Integer.MAX_VALUE; |
650 |
< |
else |
651 |
< |
return (int)sum; |
652 |
< |
} |
730 |
> |
if (check == sum) |
731 |
> |
break; |
732 |
|
} |
733 |
+ |
if (check != sum) { // Resort to locking all segments |
734 |
+ |
sum = 0; |
735 |
+ |
for (int i = 0; i < segments.length; ++i) |
736 |
+ |
segments[i].lock(); |
737 |
+ |
for (int i = 0; i < segments.length; ++i) |
738 |
+ |
sum += segments[i].count; |
739 |
+ |
for (int i = 0; i < segments.length; ++i) |
740 |
+ |
segments[i].unlock(); |
741 |
+ |
} |
742 |
+ |
if (sum > Integer.MAX_VALUE) |
743 |
+ |
return Integer.MAX_VALUE; |
744 |
+ |
else |
745 |
+ |
return (int)sum; |
746 |
|
} |
747 |
|
|
748 |
|
|
791 |
|
if (value == null) |
792 |
|
throw new NullPointerException(); |
793 |
|
|
794 |
+ |
// See explanation of modCount use above |
795 |
+ |
|
796 |
+ |
final Segment[] segments = this.segments; |
797 |
|
int[] mc = new int[segments.length]; |
798 |
< |
for (;;) { |
798 |
> |
|
799 |
> |
// Try a few times without locking |
800 |
> |
for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) { |
801 |
|
int sum = 0; |
802 |
|
int mcsum = 0; |
803 |
|
for (int i = 0; i < segments.length; ++i) { |
819 |
|
if (cleanSweep) |
820 |
|
return false; |
821 |
|
} |
822 |
+ |
// Resort to locking all segments |
823 |
+ |
for (int i = 0; i < segments.length; ++i) |
824 |
+ |
segments[i].lock(); |
825 |
+ |
boolean found = false; |
826 |
+ |
try { |
827 |
+ |
for (int i = 0; i < segments.length; ++i) { |
828 |
+ |
if (segments[i].containsValue(value)) { |
829 |
+ |
found = true; |
830 |
+ |
break; |
831 |
+ |
} |
832 |
+ |
} |
833 |
+ |
} finally { |
834 |
+ |
for (int i = 0; i < segments.length; ++i) |
835 |
+ |
segments[i].unlock(); |
836 |
+ |
} |
837 |
+ |
return found; |
838 |
|
} |
839 |
|
|
840 |
|
/** |
859 |
|
/** |
860 |
|
* Maps the specified <tt>key</tt> to the specified |
861 |
|
* <tt>value</tt> in this table. Neither the key nor the |
862 |
< |
* value can be <tt>null</tt>. <p> |
862 |
> |
* value can be <tt>null</tt>. |
863 |
|
* |
864 |
< |
* The value can be retrieved by calling the <tt>get</tt> method |
864 |
> |
* <p> The value can be retrieved by calling the <tt>get</tt> method |
865 |
|
* with a key that is equal to the original key. |
866 |
|
* |
867 |
|
* @param key the table key. |
883 |
|
* with a value, associate it with the given value. |
884 |
|
* This is equivalent to |
885 |
|
* <pre> |
886 |
< |
* if (!map.containsKey(key)) |
886 |
> |
* if (!map.containsKey(key)) |
887 |
|
* return map.put(key, value); |
888 |
|
* else |
889 |
< |
* return map.get(key); |
890 |
< |
* </pre> |
778 |
< |
* Except that the action is performed atomically. |
889 |
> |
* return map.get(key);</pre> |
890 |
> |
* except that the action is performed atomically. |
891 |
|
* @param key key with which the specified value is to be associated. |
892 |
|
* @param value value to be associated with the specified key. |
893 |
|
* @return previous value associated with specified key, or <tt>null</tt> |
894 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
783 |
< |
* also indicate that the map previously associated <tt>null</tt> |
784 |
< |
* with the specified key, if the implementation supports |
785 |
< |
* <tt>null</tt> values. |
786 |
< |
* |
787 |
< |
* @throws UnsupportedOperationException if the <tt>put</tt> operation is |
788 |
< |
* not supported by this map. |
789 |
< |
* @throws ClassCastException if the class of the specified key or value |
790 |
< |
* prevents it from being stored in this map. |
894 |
> |
* if there was no mapping for key. |
895 |
|
* @throws NullPointerException if the specified key or value is |
896 |
|
* <tt>null</tt>. |
897 |
< |
* |
794 |
< |
**/ |
897 |
> |
*/ |
898 |
|
public V putIfAbsent(K key, V value) { |
899 |
|
if (value == null) |
900 |
|
throw new NullPointerException(); |
912 |
|
* @param t Mappings to be stored in this map. |
913 |
|
*/ |
914 |
|
public void putAll(Map<? extends K, ? extends V> t) { |
915 |
< |
for (Iterator<Map.Entry<? extends K, ? extends V>> it = (Iterator<Map.Entry<? extends K, ? extends V>>) t.entrySet().iterator(); it.hasNext(); ) { |
915 |
> |
for (Iterator<? extends Map.Entry<? extends K, ? extends V>> it = (Iterator<? extends Map.Entry<? extends K, ? extends V>>) t.entrySet().iterator(); it.hasNext(); ) { |
916 |
|
Entry<? extends K, ? extends V> e = it.next(); |
917 |
|
put(e.getKey(), e.getValue()); |
918 |
|
} |
935 |
|
|
936 |
|
/** |
937 |
|
* Remove entry for key only if currently mapped to given value. |
938 |
< |
* Acts as |
939 |
< |
* <pre> |
938 |
> |
* This is equivalent to |
939 |
> |
* <pre> |
940 |
|
* if (map.get(key).equals(value)) { |
941 |
|
* map.remove(key); |
942 |
|
* return true; |
943 |
< |
* } else return false; |
841 |
< |
* </pre> |
943 |
> |
* } else return false;</pre> |
944 |
|
* except that the action is performed atomically. |
945 |
|
* @param key key with which the specified value is associated. |
946 |
|
* @param value value associated with the specified key. |
955 |
|
|
956 |
|
|
957 |
|
/** |
958 |
< |
* Replace entry for key only if currently mapped to given value. |
959 |
< |
* Acts as |
960 |
< |
* <pre> |
958 |
> |
* Replaces entry for key only if currently mapped to given value. |
959 |
> |
* This is equivalent to |
960 |
> |
* <pre> |
961 |
|
* if (map.get(key).equals(oldValue)) { |
962 |
|
* map.put(key, newValue); |
963 |
|
* return true; |
964 |
< |
* } else return false; |
863 |
< |
* </pre> |
964 |
> |
* } else return false;</pre> |
965 |
|
* except that the action is performed atomically. |
966 |
|
* @param key key with which the specified value is associated. |
967 |
|
* @param oldValue value expected to be associated with the specified key. |
978 |
|
} |
979 |
|
|
980 |
|
/** |
981 |
< |
* Replace entry for key only if currently mapped to some value. |
982 |
< |
* Acts as |
983 |
< |
* <pre> |
984 |
< |
* if ((map.containsKey(key)) { |
981 |
> |
* Replaces entry for key only if currently mapped to some value. |
982 |
> |
* This is equivalent to |
983 |
> |
* <pre> |
984 |
> |
* if (map.containsKey(key)) { |
985 |
|
* return map.put(key, value); |
986 |
< |
* } else return null; |
886 |
< |
* </pre> |
986 |
> |
* } else return null;</pre> |
987 |
|
* except that the action is performed atomically. |
988 |
|
* @param key key with which the specified value is associated. |
989 |
|
* @param value value to be associated with the specified key. |
990 |
|
* @return previous value associated with specified key, or <tt>null</tt> |
991 |
< |
* if there was no mapping for key. |
991 |
> |
* if there was no mapping for key. |
992 |
|
* @throws NullPointerException if the specified key or value is |
993 |
|
* <tt>null</tt>. |
994 |
|
*/ |
1008 |
|
segments[i].clear(); |
1009 |
|
} |
1010 |
|
|
911 |
– |
|
912 |
– |
/** |
913 |
– |
* Returns a shallow copy of this |
914 |
– |
* <tt>ConcurrentHashMap</tt> instance: the keys and |
915 |
– |
* values themselves are not cloned. |
916 |
– |
* |
917 |
– |
* @return a shallow copy of this map. |
918 |
– |
*/ |
919 |
– |
public Object clone() { |
920 |
– |
// We cannot call super.clone, since it would share final |
921 |
– |
// segments array, and there's no way to reassign finals. |
922 |
– |
|
923 |
– |
float lf = segments[0].loadFactor; |
924 |
– |
int segs = segments.length; |
925 |
– |
int cap = (int)(size() / lf); |
926 |
– |
if (cap < segs) cap = segs; |
927 |
– |
ConcurrentHashMap<K,V> t = new ConcurrentHashMap<K,V>(cap, lf, segs); |
928 |
– |
t.putAll(this); |
929 |
– |
return t; |
930 |
– |
} |
931 |
– |
|
1011 |
|
/** |
1012 |
|
* Returns a set view of the keys contained in this map. The set is |
1013 |
|
* backed by the map, so changes to the map are reflected in the set, and |
1016 |
|
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and |
1017 |
|
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
1018 |
|
* <tt>addAll</tt> operations. |
1019 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1019 |
> |
* The view's returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1020 |
|
* will never throw {@link java.util.ConcurrentModificationException}, |
1021 |
|
* and guarantees to traverse elements as they existed upon |
1022 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1038 |
|
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
1039 |
|
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
1040 |
|
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
1041 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1041 |
> |
* The view's returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1042 |
|
* will never throw {@link java.util.ConcurrentModificationException}, |
1043 |
|
* and guarantees to traverse elements as they existed upon |
1044 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1061 |
|
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
1062 |
|
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
1063 |
|
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
1064 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1064 |
> |
* The view's returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1065 |
|
* will never throw {@link java.util.ConcurrentModificationException}, |
1066 |
|
* and guarantees to traverse elements as they existed upon |
1067 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1071 |
|
*/ |
1072 |
|
public Set<Map.Entry<K,V>> entrySet() { |
1073 |
|
Set<Map.Entry<K,V>> es = entrySet; |
1074 |
< |
return (es != null) ? es : (entrySet = (Set<Map.Entry<K,V>>) (Set) new EntrySet()); |
1074 |
> |
return (es != null) ? es : (entrySet = new EntrySet()); |
1075 |
|
} |
1076 |
|
|
1077 |
|
|
1087 |
|
|
1088 |
|
/** |
1089 |
|
* Returns an enumeration of the values in this table. |
1011 |
– |
* Use the Enumeration methods on the returned object to fetch the elements |
1012 |
– |
* sequentially. |
1090 |
|
* |
1091 |
|
* @return an enumeration of the values in this table. |
1092 |
|
* @see #values |
1097 |
|
|
1098 |
|
/* ---------------- Iterator Support -------------- */ |
1099 |
|
|
1100 |
< |
private abstract class HashIterator { |
1101 |
< |
private int nextSegmentIndex; |
1102 |
< |
private int nextTableIndex; |
1103 |
< |
private HashEntry[] currentTable; |
1104 |
< |
private HashEntry<K, V> nextEntry; |
1100 |
> |
abstract class HashIterator { |
1101 |
> |
int nextSegmentIndex; |
1102 |
> |
int nextTableIndex; |
1103 |
> |
HashEntry[] currentTable; |
1104 |
> |
HashEntry<K, V> nextEntry; |
1105 |
|
HashEntry<K, V> lastReturned; |
1106 |
|
|
1107 |
< |
private HashIterator() { |
1107 |
> |
HashIterator() { |
1108 |
|
nextSegmentIndex = segments.length - 1; |
1109 |
|
nextTableIndex = -1; |
1110 |
|
advance(); |
1112 |
|
|
1113 |
|
public boolean hasMoreElements() { return hasNext(); } |
1114 |
|
|
1115 |
< |
private void advance() { |
1115 |
> |
final void advance() { |
1116 |
|
if (nextEntry != null && (nextEntry = nextEntry.next) != null) |
1117 |
|
return; |
1118 |
|
|
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
1156 |
< |
private class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> { |
1156 |
> |
final class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> { |
1157 |
|
public K next() { return super.nextEntry().key; } |
1158 |
|
public K nextElement() { return super.nextEntry().key; } |
1159 |
|
} |
1160 |
|
|
1161 |
< |
private class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> { |
1161 |
> |
final class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> { |
1162 |
|
public V next() { return super.nextEntry().value; } |
1163 |
|
public V nextElement() { return super.nextEntry().value; } |
1164 |
|
} |
1165 |
|
|
1166 |
< |
|
1166 |
> |
|
1167 |
|
|
1168 |
|
/** |
1169 |
< |
* Exported Entry objects must write-through changes in setValue, |
1170 |
< |
* even if the nodes have been cloned. So we cannot return |
1171 |
< |
* internal HashEntry objects. Instead, the iterator itself acts |
1172 |
< |
* as a forwarding pseudo-entry. |
1169 |
> |
* Entry iterator. Exported Entry objects must write-through |
1170 |
> |
* changes in setValue, even if the nodes have been cloned. So we |
1171 |
> |
* cannot return internal HashEntry objects. Instead, the iterator |
1172 |
> |
* itself acts as a forwarding pseudo-entry. |
1173 |
|
*/ |
1174 |
< |
private class EntryIterator extends HashIterator implements Map.Entry<K,V>, Iterator<Entry<K,V>> { |
1174 |
> |
final class EntryIterator extends HashIterator implements Map.Entry<K,V>, Iterator<Entry<K,V>> { |
1175 |
|
public Map.Entry<K,V> next() { |
1176 |
|
nextEntry(); |
1177 |
|
return this; |
1196 |
|
} |
1197 |
|
|
1198 |
|
public boolean equals(Object o) { |
1199 |
+ |
// If not acting as entry, just use default. |
1200 |
+ |
if (lastReturned == null) |
1201 |
+ |
return super.equals(o); |
1202 |
|
if (!(o instanceof Map.Entry)) |
1203 |
|
return false; |
1204 |
< |
Map.Entry e = (Map.Entry)o; |
1205 |
< |
return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue()); |
1206 |
< |
} |
1204 |
> |
Map.Entry e = (Map.Entry)o; |
1205 |
> |
return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue()); |
1206 |
> |
} |
1207 |
|
|
1208 |
|
public int hashCode() { |
1209 |
+ |
// If not acting as entry, just use default. |
1210 |
+ |
if (lastReturned == null) |
1211 |
+ |
return super.hashCode(); |
1212 |
+ |
|
1213 |
|
Object k = getKey(); |
1214 |
|
Object v = getValue(); |
1215 |
|
return ((k == null) ? 0 : k.hashCode()) ^ |
1217 |
|
} |
1218 |
|
|
1219 |
|
public String toString() { |
1220 |
< |
return getKey() + "=" + getValue(); |
1220 |
> |
// If not acting as entry, just use default. |
1221 |
> |
if (lastReturned == null) |
1222 |
> |
return super.toString(); |
1223 |
> |
else |
1224 |
> |
return getKey() + "=" + getValue(); |
1225 |
|
} |
1226 |
|
|
1227 |
< |
private boolean eq(Object o1, Object o2) { |
1227 |
> |
boolean eq(Object o1, Object o2) { |
1228 |
|
return (o1 == null ? o2 == null : o1.equals(o2)); |
1229 |
|
} |
1230 |
|
|
1231 |
|
} |
1232 |
|
|
1233 |
< |
private class KeySet extends AbstractSet<K> { |
1233 |
> |
final class KeySet extends AbstractSet<K> { |
1234 |
|
public Iterator<K> iterator() { |
1235 |
|
return new KeyIterator(); |
1236 |
|
} |
1246 |
|
public void clear() { |
1247 |
|
ConcurrentHashMap.this.clear(); |
1248 |
|
} |
1249 |
+ |
public Object[] toArray() { |
1250 |
+ |
Collection<K> c = new ArrayList<K>(); |
1251 |
+ |
for (Iterator<K> i = iterator(); i.hasNext(); ) |
1252 |
+ |
c.add(i.next()); |
1253 |
+ |
return c.toArray(); |
1254 |
+ |
} |
1255 |
+ |
public <T> T[] toArray(T[] a) { |
1256 |
+ |
Collection<K> c = new ArrayList<K>(); |
1257 |
+ |
for (Iterator<K> i = iterator(); i.hasNext(); ) |
1258 |
+ |
c.add(i.next()); |
1259 |
+ |
return c.toArray(a); |
1260 |
+ |
} |
1261 |
|
} |
1262 |
|
|
1263 |
< |
private class Values extends AbstractCollection<V> { |
1263 |
> |
final class Values extends AbstractCollection<V> { |
1264 |
|
public Iterator<V> iterator() { |
1265 |
|
return new ValueIterator(); |
1266 |
|
} |
1273 |
|
public void clear() { |
1274 |
|
ConcurrentHashMap.this.clear(); |
1275 |
|
} |
1276 |
+ |
public Object[] toArray() { |
1277 |
+ |
Collection<V> c = new ArrayList<V>(); |
1278 |
+ |
for (Iterator<V> i = iterator(); i.hasNext(); ) |
1279 |
+ |
c.add(i.next()); |
1280 |
+ |
return c.toArray(); |
1281 |
+ |
} |
1282 |
+ |
public <T> T[] toArray(T[] a) { |
1283 |
+ |
Collection<V> c = new ArrayList<V>(); |
1284 |
+ |
for (Iterator<V> i = iterator(); i.hasNext(); ) |
1285 |
+ |
c.add(i.next()); |
1286 |
+ |
return c.toArray(a); |
1287 |
+ |
} |
1288 |
|
} |
1289 |
|
|
1290 |
< |
private class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
1290 |
> |
final class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
1291 |
|
public Iterator<Map.Entry<K,V>> iterator() { |
1292 |
|
return new EntryIterator(); |
1293 |
|
} |
1315 |
|
// must pack elements using exportable SimpleEntry |
1316 |
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1317 |
|
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1318 |
< |
c.add(new SimpleEntry<K,V>(i.next())); |
1318 |
> |
c.add(new AbstractMap.SimpleEntry<K,V>(i.next())); |
1319 |
|
return c.toArray(); |
1320 |
|
} |
1321 |
|
public <T> T[] toArray(T[] a) { |
1322 |
|
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1323 |
|
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1324 |
< |
c.add(new SimpleEntry<K,V>(i.next())); |
1324 |
> |
c.add(new AbstractMap.SimpleEntry<K,V>(i.next())); |
1325 |
|
return c.toArray(a); |
1326 |
|
} |
1327 |
|
|
1328 |
|
} |
1329 |
|
|
1218 |
– |
/** |
1219 |
– |
* This duplicates java.util.AbstractMap.SimpleEntry until this class |
1220 |
– |
* is made accessible. |
1221 |
– |
*/ |
1222 |
– |
static class SimpleEntry<K,V> implements Entry<K,V> { |
1223 |
– |
K key; |
1224 |
– |
V value; |
1225 |
– |
|
1226 |
– |
public SimpleEntry(K key, V value) { |
1227 |
– |
this.key = key; |
1228 |
– |
this.value = value; |
1229 |
– |
} |
1230 |
– |
|
1231 |
– |
public SimpleEntry(Entry<K,V> e) { |
1232 |
– |
this.key = e.getKey(); |
1233 |
– |
this.value = e.getValue(); |
1234 |
– |
} |
1235 |
– |
|
1236 |
– |
public K getKey() { |
1237 |
– |
return key; |
1238 |
– |
} |
1239 |
– |
|
1240 |
– |
public V getValue() { |
1241 |
– |
return value; |
1242 |
– |
} |
1243 |
– |
|
1244 |
– |
public V setValue(V value) { |
1245 |
– |
V oldValue = this.value; |
1246 |
– |
this.value = value; |
1247 |
– |
return oldValue; |
1248 |
– |
} |
1249 |
– |
|
1250 |
– |
public boolean equals(Object o) { |
1251 |
– |
if (!(o instanceof Map.Entry)) |
1252 |
– |
return false; |
1253 |
– |
Map.Entry e = (Map.Entry)o; |
1254 |
– |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
1255 |
– |
} |
1256 |
– |
|
1257 |
– |
public int hashCode() { |
1258 |
– |
return ((key == null) ? 0 : key.hashCode()) ^ |
1259 |
– |
((value == null) ? 0 : value.hashCode()); |
1260 |
– |
} |
1261 |
– |
|
1262 |
– |
public String toString() { |
1263 |
– |
return key + "=" + value; |
1264 |
– |
} |
1265 |
– |
|
1266 |
– |
private static boolean eq(Object o1, Object o2) { |
1267 |
– |
return (o1 == null ? o2 == null : o1.equals(o2)); |
1268 |
– |
} |
1269 |
– |
} |
1270 |
– |
|
1330 |
|
/* ---------------- Serialization Support -------------- */ |
1331 |
|
|
1332 |
|
/** |