1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
33 |
|
* 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 |
< |
* They do <em>not</em> throw |
37 |
< |
* {@link ConcurrentModificationException}. However, iterators are |
38 |
< |
* designed to be used by only one thread at a time. |
36 |
> |
* They do <em>not</em> throw {@link ConcurrentModificationException}. |
37 |
> |
* However, iterators are designed to be used by only one thread at a time. |
38 |
|
* |
39 |
|
* <p> The allowed concurrency among update operations is guided by |
40 |
|
* the optional <tt>concurrencyLevel</tt> constructor argument |
41 |
< |
* (default 16), which is used as a hint for internal sizing. The |
41 |
> |
* (default <tt>16</tt>), which is used as a hint for internal sizing. The |
42 |
|
* table is internally partitioned to try to permit the indicated |
43 |
|
* number of concurrent updates without contention. Because placement |
44 |
|
* in hash tables is essentially random, the actual concurrency will |
58 |
|
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
59 |
|
* interfaces. |
60 |
|
* |
61 |
< |
* <p> Like {@link java.util.Hashtable} but unlike {@link |
62 |
< |
* java.util.HashMap}, this class does NOT allow <tt>null</tt> to be |
64 |
< |
* used as a key or value. |
61 |
> |
* <p> Like {@link Hashtable} but unlike {@link HashMap}, this class |
62 |
> |
* does <em>not</em> allow <tt>null</tt> to be used as a key or value. |
63 |
|
* |
64 |
|
* <p>This class is a member of the |
65 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
65 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
66 |
|
* Java Collections Framework</a>. |
67 |
|
* |
68 |
|
* @since 1.5 |
69 |
|
* @author Doug Lea |
70 |
|
* @param <K> the type of keys maintained by this map |
71 |
< |
* @param <V> the type of mapped values |
71 |
> |
* @param <V> the type of mapped values |
72 |
|
*/ |
73 |
|
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> |
74 |
< |
implements ConcurrentMap<K, V>, Cloneable, Serializable { |
74 |
> |
implements ConcurrentMap<K, V>, Serializable { |
75 |
|
private static final long serialVersionUID = 7249069246763182397L; |
76 |
|
|
77 |
|
/* |
78 |
|
* The basic strategy is to subdivide the table among Segments, |
79 |
< |
* each of which itself is a concurrently readable hash table. |
79 |
> |
* each of which itself is a concurrently readable hash table. To |
80 |
> |
* reduce footprint, all but one segments are constructed only |
81 |
> |
* when first needed (see ensureSegment). To maintain visibility |
82 |
> |
* in the presence of lazy construction, accesses to segments as |
83 |
> |
* well as elements of segment's table must use volatile access, |
84 |
> |
* which is done via Unsafe within methods segmentAt etc |
85 |
> |
* below. These provide the functionality of AtomicReferenceArrays |
86 |
> |
* but reduce the levels of indirection. Additionally, |
87 |
> |
* volatile-writes of table elements and entry "next" fields |
88 |
> |
* within locked operations use the cheaper "lazySet" forms of |
89 |
> |
* writes (via putOrderedObject) because these writes are always |
90 |
> |
* followed by lock releases that maintain sequential consistency |
91 |
> |
* of table updates. |
92 |
> |
* |
93 |
> |
* Historical note: The previous version of this class relied |
94 |
> |
* heavily on "final" fields, which avoided some volatile reads at |
95 |
> |
* the expense of a large initial footprint. Some remnants of |
96 |
> |
* that design (including forced construction of segment 0) exist |
97 |
> |
* to ensure serialization compatibility. |
98 |
|
*/ |
99 |
|
|
100 |
|
/* ---------------- Constants -------------- */ |
101 |
|
|
102 |
|
/** |
103 |
< |
* The default initial number of table slots for this table. |
104 |
< |
* Used when not otherwise specified in constructor. |
103 |
> |
* The default initial capacity for this table, |
104 |
> |
* used when not otherwise specified in a constructor. |
105 |
> |
*/ |
106 |
> |
static final int DEFAULT_INITIAL_CAPACITY = 16; |
107 |
> |
|
108 |
> |
/** |
109 |
> |
* The default load factor for this table, used when not |
110 |
> |
* otherwise specified in a constructor. |
111 |
> |
*/ |
112 |
> |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
113 |
> |
|
114 |
> |
/** |
115 |
> |
* The default concurrency level for this table, used when not |
116 |
> |
* otherwise specified in a constructor. |
117 |
|
*/ |
118 |
< |
static int DEFAULT_INITIAL_CAPACITY = 16; |
118 |
> |
static final int DEFAULT_CONCURRENCY_LEVEL = 16; |
119 |
|
|
120 |
|
/** |
121 |
|
* The maximum capacity, used if a higher value is implicitly |
122 |
|
* specified by either of the constructors with arguments. MUST |
123 |
< |
* be a power of two <= 1<<30 to ensure that entries are indexible |
123 |
> |
* be a power of two <= 1<<30 to ensure that entries are indexable |
124 |
|
* using ints. |
125 |
|
*/ |
126 |
< |
static final int MAXIMUM_CAPACITY = 1 << 30; |
126 |
> |
static final int MAXIMUM_CAPACITY = 1 << 30; |
127 |
|
|
128 |
|
/** |
129 |
< |
* The default load factor for this table. Used when not |
130 |
< |
* otherwise specified in constructor. |
129 |
> |
* The minimum capacity for per-segment tables. Must be a power |
130 |
> |
* of two, at least two to avoid immediate resizing on next use |
131 |
> |
* after lazy construction. |
132 |
|
*/ |
133 |
< |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
105 |
< |
|
106 |
< |
/** |
107 |
< |
* The default number of concurrency control segments. |
108 |
< |
**/ |
109 |
< |
static final int DEFAULT_SEGMENTS = 16; |
133 |
> |
static final int MIN_SEGMENT_TABLE_CAPACITY = 2; |
134 |
|
|
135 |
|
/** |
136 |
|
* The maximum number of segments to allow; used to bound |
137 |
< |
* constructor arguments. |
137 |
> |
* constructor arguments. Must be power of two less than 1 << 24. |
138 |
|
*/ |
139 |
|
static final int MAX_SEGMENTS = 1 << 16; // slightly conservative |
140 |
|
|
141 |
+ |
/** |
142 |
+ |
* Number of unsynchronized retries in size and containsValue |
143 |
+ |
* methods before resorting to locking. This is used to avoid |
144 |
+ |
* unbounded retries if tables undergo continuous modification |
145 |
+ |
* which would make it impossible to obtain an accurate result. |
146 |
+ |
*/ |
147 |
+ |
static final int RETRIES_BEFORE_LOCK = 2; |
148 |
+ |
|
149 |
|
/* ---------------- Fields -------------- */ |
150 |
|
|
151 |
|
/** |
152 |
|
* Mask value for indexing into segments. The upper bits of a |
153 |
|
* key's hash code are used to choose the segment. |
154 |
< |
**/ |
154 |
> |
*/ |
155 |
|
final int segmentMask; |
156 |
|
|
157 |
|
/** |
158 |
|
* Shift value for indexing within segments. |
159 |
< |
**/ |
159 |
> |
*/ |
160 |
|
final int segmentShift; |
161 |
|
|
162 |
|
/** |
163 |
< |
* The segments, each of which is a specialized hash table |
163 |
> |
* The segments, each of which is a specialized hash table. |
164 |
|
*/ |
165 |
< |
final Segment[] segments; |
165 |
> |
final Segment<K,V>[] segments; |
166 |
|
|
167 |
|
transient Set<K> keySet; |
168 |
|
transient Set<Map.Entry<K,V>> entrySet; |
169 |
|
transient Collection<V> values; |
170 |
|
|
139 |
– |
/* ---------------- Small Utilities -------------- */ |
140 |
– |
|
171 |
|
/** |
172 |
< |
* Returns a hash code for non-null Object x. |
173 |
< |
* Uses the same hash code spreader as most other java.util hash tables. |
144 |
< |
* @param x the object serving as a key |
145 |
< |
* @return the hash code |
172 |
> |
* ConcurrentHashMap list entry. Note that this is never exported |
173 |
> |
* out as a user-visible Map.Entry. |
174 |
|
*/ |
175 |
< |
static int hash(Object x) { |
176 |
< |
int h = x.hashCode(); |
177 |
< |
h += ~(h << 9); |
178 |
< |
h ^= (h >>> 14); |
179 |
< |
h += (h << 4); |
180 |
< |
h ^= (h >>> 10); |
181 |
< |
return h; |
175 |
> |
static final class HashEntry<K,V> { |
176 |
> |
final int hash; |
177 |
> |
final K key; |
178 |
> |
volatile V value; |
179 |
> |
volatile HashEntry<K,V> next; |
180 |
> |
|
181 |
> |
HashEntry(int hash, K key, V value, HashEntry<K,V> next) { |
182 |
> |
this.hash = hash; |
183 |
> |
this.key = key; |
184 |
> |
this.value = value; |
185 |
> |
this.next = next; |
186 |
> |
} |
187 |
> |
|
188 |
> |
/** |
189 |
> |
* Sets next field with volatile write semantics. (See above |
190 |
> |
* about use of putOrderedObject.) |
191 |
> |
*/ |
192 |
> |
final void setNext(HashEntry<K,V> n) { |
193 |
> |
UNSAFE.putOrderedObject(this, nextOffset, n); |
194 |
> |
} |
195 |
> |
|
196 |
> |
// Unsafe mechanics |
197 |
> |
static final sun.misc.Unsafe UNSAFE; |
198 |
> |
static final long nextOffset; |
199 |
> |
static { |
200 |
> |
try { |
201 |
> |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
202 |
> |
Class<?> k = HashEntry.class; |
203 |
> |
nextOffset = UNSAFE.objectFieldOffset |
204 |
> |
(k.getDeclaredField("next")); |
205 |
> |
} catch (Exception e) { |
206 |
> |
throw new Error(e); |
207 |
> |
} |
208 |
> |
} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
< |
* Returns the segment that should be used for key with given hash |
213 |
< |
* @param hash the hash code for the key |
214 |
< |
* @return the segment |
215 |
< |
*/ |
216 |
< |
final Segment<K,V> segmentFor(int hash) { |
217 |
< |
return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask]; |
212 |
> |
* Gets the ith element of given table (if nonnull) with volatile |
213 |
> |
* read semantics. Note: This is manually integrated into a few |
214 |
> |
* performance-sensitive methods to reduce call overhead. |
215 |
> |
*/ |
216 |
> |
@SuppressWarnings("unchecked") |
217 |
> |
static final <K,V> HashEntry<K,V> entryAt(HashEntry<K,V>[] tab, int i) { |
218 |
> |
return (tab == null) ? null : |
219 |
> |
(HashEntry<K,V>) UNSAFE.getObjectVolatile |
220 |
> |
(tab, ((long)i << TSHIFT) + TBASE); |
221 |
> |
} |
222 |
> |
|
223 |
> |
/** |
224 |
> |
* Sets the ith element of given table, with volatile write |
225 |
> |
* semantics. (See above about use of putOrderedObject.) |
226 |
> |
*/ |
227 |
> |
static final <K,V> void setEntryAt(HashEntry<K,V>[] tab, int i, |
228 |
> |
HashEntry<K,V> e) { |
229 |
> |
UNSAFE.putOrderedObject(tab, ((long)i << TSHIFT) + TBASE, e); |
230 |
|
} |
231 |
|
|
232 |
< |
/* ---------------- Inner Classes -------------- */ |
232 |
> |
/** |
233 |
> |
* Applies a supplemental hash function to a given hashCode, which |
234 |
> |
* defends against poor quality hash functions. This is critical |
235 |
> |
* because ConcurrentHashMap uses power-of-two length hash tables, |
236 |
> |
* that otherwise encounter collisions for hashCodes that do not |
237 |
> |
* differ in lower or upper bits. |
238 |
> |
*/ |
239 |
> |
private static int hash(int h) { |
240 |
> |
// Spread bits to regularize both segment and index locations, |
241 |
> |
// using variant of single-word Wang/Jenkins hash. |
242 |
> |
h += (h << 15) ^ 0xffffcd7d; |
243 |
> |
h ^= (h >>> 10); |
244 |
> |
h += (h << 3); |
245 |
> |
h ^= (h >>> 6); |
246 |
> |
h += (h << 2) + (h << 14); |
247 |
> |
return h ^ (h >>> 16); |
248 |
> |
} |
249 |
|
|
250 |
|
/** |
251 |
|
* Segments are specialized versions of hash tables. This |
252 |
|
* subclasses from ReentrantLock opportunistically, just to |
253 |
|
* simplify some locking and avoid separate construction. |
254 |
< |
**/ |
254 |
> |
*/ |
255 |
|
static final class Segment<K,V> extends ReentrantLock implements Serializable { |
256 |
|
/* |
257 |
< |
* Segments maintain a table of entry lists that are ALWAYS |
258 |
< |
* kept in a consistent state, so can be read without locking. |
259 |
< |
* Next fields of nodes are immutable (final). All list |
260 |
< |
* additions are performed at the front of each bin. This |
261 |
< |
* makes it easy to check changes, and also fast to traverse. |
262 |
< |
* When nodes would otherwise be changed, new nodes are |
180 |
< |
* created to replace them. This works well for hash tables |
181 |
< |
* since the bin lists tend to be short. (The average length |
182 |
< |
* is less than two for the default load factor threshold.) |
183 |
< |
* |
184 |
< |
* Read operations can thus proceed without locking, but rely |
185 |
< |
* on selected uses of volatiles to ensure that completed |
186 |
< |
* write operations performed by other threads are |
187 |
< |
* noticed. For most purposes, the "count" field, tracking the |
188 |
< |
* number of elements, serves as that volatile variable |
189 |
< |
* ensuring visibility. This is convenient because this field |
190 |
< |
* needs to be read in many read operations anyway: |
257 |
> |
* Segments maintain a table of entry lists that are always |
258 |
> |
* kept in a consistent state, so can be read (via volatile |
259 |
> |
* reads of segments and tables) without locking. This |
260 |
> |
* requires replicating nodes when necessary during table |
261 |
> |
* resizing, so the old lists can be traversed by readers |
262 |
> |
* still using old version of table. |
263 |
|
* |
264 |
< |
* - All (unsynchronized) read operations must first read the |
265 |
< |
* "count" field, and should not look at table entries if |
266 |
< |
* it is 0. |
267 |
< |
* |
268 |
< |
* - All (synchronized) write operations should write to |
269 |
< |
* the "count" field after structurally changing any bin. |
270 |
< |
* The operations must not take any action that could even |
271 |
< |
* momentarily cause a concurrent read operation to see |
272 |
< |
* inconsistent data. This is made easier by the nature of |
273 |
< |
* the read operations in Map. For example, no operation |
274 |
< |
* can reveal that the table has grown but the threshold |
275 |
< |
* has not yet been updated, so there are no atomicity |
276 |
< |
* requirements for this with respect to reads. |
277 |
< |
* |
278 |
< |
* As a guide, all critical volatile reads and writes to the |
279 |
< |
* count field are marked in code comments. |
264 |
> |
* This class defines only mutative methods requiring locking. |
265 |
> |
* Except as noted, the methods of this class perform the |
266 |
> |
* per-segment versions of ConcurrentHashMap methods. (Other |
267 |
> |
* methods are integrated directly into ConcurrentHashMap |
268 |
> |
* methods.) These mutative methods use a form of controlled |
269 |
> |
* spinning on contention via methods scanAndLock and |
270 |
> |
* scanAndLockForPut. These intersperse tryLocks with |
271 |
> |
* traversals to locate nodes. The main benefit is to absorb |
272 |
> |
* cache misses (which are very common for hash tables) while |
273 |
> |
* obtaining locks so that traversal is faster once |
274 |
> |
* acquired. We do not actually use the found nodes since they |
275 |
> |
* must be re-acquired under lock anyway to ensure sequential |
276 |
> |
* consistency of updates (and in any case may be undetectably |
277 |
> |
* stale), but they will normally be much faster to re-locate. |
278 |
> |
* Also, scanAndLockForPut speculatively creates a fresh node |
279 |
> |
* to use in put if no node is found. |
280 |
|
*/ |
281 |
|
|
282 |
|
private static final long serialVersionUID = 2249069246763182397L; |
283 |
|
|
284 |
|
/** |
285 |
< |
* The number of elements in this segment's region. |
286 |
< |
**/ |
287 |
< |
transient volatile int count; |
285 |
> |
* The maximum number of times to tryLock in a prescan before |
286 |
> |
* possibly blocking on acquire in preparation for a locked |
287 |
> |
* segment operation. On multiprocessors, using a bounded |
288 |
> |
* number of retries maintains cache acquired while locating |
289 |
> |
* nodes. |
290 |
> |
*/ |
291 |
> |
static final int MAX_SCAN_RETRIES = |
292 |
> |
Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1; |
293 |
> |
|
294 |
> |
/** |
295 |
> |
* The per-segment table. Elements are accessed via |
296 |
> |
* entryAt/setEntryAt providing volatile semantics. |
297 |
> |
*/ |
298 |
> |
transient volatile HashEntry<K,V>[] table; |
299 |
|
|
300 |
|
/** |
301 |
< |
* Number of updates that alter the size of the table. This is |
302 |
< |
* used during bulk-read methods to make sure they see a |
220 |
< |
* consistent snapshot: If modCounts change during a traversal |
221 |
< |
* of segments computing size or checking contatinsValue, then |
222 |
< |
* we might have an inconsistent view of state so (usually) |
223 |
< |
* must retry. |
301 |
> |
* The number of elements. Accessed only either within locks |
302 |
> |
* or among other volatile reads that maintain visibility. |
303 |
|
*/ |
304 |
< |
transient int modCount; |
304 |
> |
transient int count; |
305 |
|
|
306 |
|
/** |
307 |
< |
* The table is rehashed when its size exceeds this threshold. |
308 |
< |
* (The value of this field is always (int)(capacity * |
309 |
< |
* loadFactor).) |
307 |
> |
* The total number of mutative operations in this segment. |
308 |
> |
* Even though this may overflows 32 bits, it provides |
309 |
> |
* sufficient accuracy for stability checks in CHM isEmpty() |
310 |
> |
* and size() methods. Accessed only either within locks or |
311 |
> |
* among other volatile reads that maintain visibility. |
312 |
|
*/ |
313 |
< |
transient int threshold; |
313 |
> |
transient int modCount; |
314 |
|
|
315 |
|
/** |
316 |
< |
* The per-segment table. Declared as a raw type, casted |
317 |
< |
* to HashEntry<K,V> on each use. |
316 |
> |
* The table is rehashed when its size exceeds this threshold. |
317 |
> |
* (The value of this field is always <tt>(int)(capacity * |
318 |
> |
* loadFactor)</tt>.) |
319 |
|
*/ |
320 |
< |
transient volatile HashEntry[] table; |
320 |
> |
transient int threshold; |
321 |
|
|
322 |
|
/** |
323 |
|
* The load factor for the hash table. Even though this value |
327 |
|
*/ |
328 |
|
final float loadFactor; |
329 |
|
|
330 |
< |
Segment(int initialCapacity, float lf) { |
331 |
< |
loadFactor = lf; |
332 |
< |
setTable(new HashEntry[initialCapacity]); |
333 |
< |
} |
252 |
< |
|
253 |
< |
/** |
254 |
< |
* Set table to new HashEntry array. |
255 |
< |
* Call only while holding lock or in constructor. |
256 |
< |
**/ |
257 |
< |
void setTable(HashEntry[] newTable) { |
258 |
< |
threshold = (int)(newTable.length * loadFactor); |
259 |
< |
table = newTable; |
260 |
< |
} |
261 |
< |
|
262 |
< |
/** |
263 |
< |
* Return properly casted first entry of bin for given hash |
264 |
< |
*/ |
265 |
< |
HashEntry<K,V> getFirst(int hash) { |
266 |
< |
HashEntry[] tab = table; |
267 |
< |
return (HashEntry<K,V>) tab[hash & (tab.length - 1)]; |
330 |
> |
Segment(float lf, int threshold, HashEntry<K,V>[] tab) { |
331 |
> |
this.loadFactor = lf; |
332 |
> |
this.threshold = threshold; |
333 |
> |
this.table = tab; |
334 |
|
} |
335 |
|
|
336 |
< |
/** |
337 |
< |
* Read value field of an entry under lock. Called if value |
338 |
< |
* field ever appears to be null. This is possible only if a |
339 |
< |
* compiler happens to reorder a HashEntry initialization with |
274 |
< |
* its table assignment, which is legal under memory model |
275 |
< |
* but is not known to ever occur. |
276 |
< |
*/ |
277 |
< |
V readValueUnderLock(HashEntry<K,V> e) { |
278 |
< |
lock(); |
336 |
> |
final V put(K key, int hash, V value, boolean onlyIfAbsent) { |
337 |
> |
HashEntry<K,V> node = tryLock() ? null : |
338 |
> |
scanAndLockForPut(key, hash, value); |
339 |
> |
V oldValue; |
340 |
|
try { |
341 |
< |
return e.value; |
342 |
< |
} finally { |
343 |
< |
unlock(); |
344 |
< |
} |
345 |
< |
} |
346 |
< |
|
347 |
< |
/* Specialized implementations of map methods */ |
348 |
< |
|
349 |
< |
V get(Object key, int hash) { |
350 |
< |
if (count != 0) { // read-volatile |
351 |
< |
HashEntry<K,V> e = getFirst(hash); |
352 |
< |
while (e != null) { |
353 |
< |
if (e.hash == hash && key.equals(e.key)) { |
354 |
< |
V v = e.value; |
355 |
< |
if (v != null) |
356 |
< |
return v; |
296 |
< |
return readValueUnderLock(e); // recheck |
341 |
> |
HashEntry<K,V>[] tab = table; |
342 |
> |
int index = (tab.length - 1) & hash; |
343 |
> |
HashEntry<K,V> first = entryAt(tab, index); |
344 |
> |
for (HashEntry<K,V> e = first;;) { |
345 |
> |
if (e != null) { |
346 |
> |
K k; |
347 |
> |
if ((k = e.key) == key || |
348 |
> |
(e.hash == hash && key.equals(k))) { |
349 |
> |
oldValue = e.value; |
350 |
> |
if (!onlyIfAbsent) { |
351 |
> |
e.value = value; |
352 |
> |
++modCount; |
353 |
> |
} |
354 |
> |
break; |
355 |
> |
} |
356 |
> |
e = e.next; |
357 |
|
} |
358 |
< |
e = e.next; |
359 |
< |
} |
360 |
< |
} |
361 |
< |
return null; |
362 |
< |
} |
363 |
< |
|
364 |
< |
boolean containsKey(Object key, int hash) { |
365 |
< |
if (count != 0) { // read-volatile |
366 |
< |
HashEntry<K,V> e = getFirst(hash); |
367 |
< |
while (e != null) { |
368 |
< |
if (e.hash == hash && key.equals(e.key)) |
369 |
< |
return true; |
370 |
< |
e = e.next; |
371 |
< |
} |
312 |
< |
} |
313 |
< |
return false; |
314 |
< |
} |
315 |
< |
|
316 |
< |
boolean containsValue(Object value) { |
317 |
< |
if (count != 0) { // read-volatile |
318 |
< |
HashEntry[] tab = table; |
319 |
< |
int len = tab.length; |
320 |
< |
for (int i = 0 ; i < len; i++) { |
321 |
< |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; |
322 |
< |
e != null ; |
323 |
< |
e = e.next) { |
324 |
< |
V v = e.value; |
325 |
< |
if (v == null) // recheck |
326 |
< |
v = readValueUnderLock(e); |
327 |
< |
if (value.equals(v)) |
328 |
< |
return true; |
358 |
> |
else { |
359 |
> |
if (node != null) |
360 |
> |
node.setNext(first); |
361 |
> |
else |
362 |
> |
node = new HashEntry<K,V>(hash, key, value, first); |
363 |
> |
int c = count + 1; |
364 |
> |
if (c > threshold && tab.length < MAXIMUM_CAPACITY) |
365 |
> |
rehash(node); |
366 |
> |
else |
367 |
> |
setEntryAt(tab, index, node); |
368 |
> |
++modCount; |
369 |
> |
count = c; |
370 |
> |
oldValue = null; |
371 |
> |
break; |
372 |
|
} |
373 |
|
} |
331 |
– |
} |
332 |
– |
return false; |
333 |
– |
} |
334 |
– |
|
335 |
– |
boolean replace(K key, int hash, V oldValue, V newValue) { |
336 |
– |
lock(); |
337 |
– |
try { |
338 |
– |
HashEntry<K,V> e = getFirst(hash); |
339 |
– |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
340 |
– |
e = e.next; |
341 |
– |
|
342 |
– |
boolean replaced = false; |
343 |
– |
if (e != null && oldValue.equals(e.value)) { |
344 |
– |
replaced = true; |
345 |
– |
e.value = newValue; |
346 |
– |
} |
347 |
– |
return replaced; |
348 |
– |
} finally { |
349 |
– |
unlock(); |
350 |
– |
} |
351 |
– |
} |
352 |
– |
|
353 |
– |
V replace(K key, int hash, V newValue) { |
354 |
– |
lock(); |
355 |
– |
try { |
356 |
– |
HashEntry<K,V> e = getFirst(hash); |
357 |
– |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
358 |
– |
e = e.next; |
359 |
– |
|
360 |
– |
V oldValue = null; |
361 |
– |
if (e != null) { |
362 |
– |
oldValue = e.value; |
363 |
– |
e.value = newValue; |
364 |
– |
} |
365 |
– |
return oldValue; |
366 |
– |
} finally { |
367 |
– |
unlock(); |
368 |
– |
} |
369 |
– |
} |
370 |
– |
|
371 |
– |
|
372 |
– |
V put(K key, int hash, V value, boolean onlyIfAbsent) { |
373 |
– |
lock(); |
374 |
– |
try { |
375 |
– |
int c = count; |
376 |
– |
if (c++ > threshold) // ensure capacity |
377 |
– |
rehash(); |
378 |
– |
HashEntry[] tab = table; |
379 |
– |
int index = hash & (tab.length - 1); |
380 |
– |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
381 |
– |
HashEntry<K,V> e = first; |
382 |
– |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
383 |
– |
e = e.next; |
384 |
– |
|
385 |
– |
V oldValue; |
386 |
– |
if (e != null) { |
387 |
– |
oldValue = e.value; |
388 |
– |
if (!onlyIfAbsent) |
389 |
– |
e.value = value; |
390 |
– |
} |
391 |
– |
else { |
392 |
– |
oldValue = null; |
393 |
– |
++modCount; |
394 |
– |
tab[index] = new HashEntry<K,V>(key, hash, first, value); |
395 |
– |
count = c; // write-volatile |
396 |
– |
} |
397 |
– |
return oldValue; |
374 |
|
} finally { |
375 |
|
unlock(); |
376 |
|
} |
377 |
+ |
return oldValue; |
378 |
|
} |
379 |
|
|
380 |
< |
void rehash() { |
381 |
< |
HashEntry[] oldTable = table; |
382 |
< |
int oldCapacity = oldTable.length; |
383 |
< |
if (oldCapacity >= MAXIMUM_CAPACITY) |
384 |
< |
return; |
385 |
< |
|
380 |
> |
/** |
381 |
> |
* Doubles size of table and repacks entries, also adding the |
382 |
> |
* given node to new table |
383 |
> |
*/ |
384 |
> |
@SuppressWarnings("unchecked") |
385 |
> |
private void rehash(HashEntry<K,V> node) { |
386 |
|
/* |
387 |
< |
* Reclassify nodes in each list to new Map. Because we are |
388 |
< |
* using power-of-two expansion, the elements from each bin |
389 |
< |
* must either stay at same index, or move with a power of two |
390 |
< |
* offset. We eliminate unnecessary node creation by catching |
391 |
< |
* cases where old nodes can be reused because their next |
392 |
< |
* fields won't change. Statistically, at the default |
393 |
< |
* threshold, only about one-sixth of them need cloning when |
394 |
< |
* a table doubles. The nodes they replace will be garbage |
395 |
< |
* collectable as soon as they are no longer referenced by any |
396 |
< |
* reader thread that may be in the midst of traversing table |
397 |
< |
* right now. |
387 |
> |
* Reclassify nodes in each list to new table. Because we |
388 |
> |
* are using power-of-two expansion, the elements from |
389 |
> |
* each bin must either stay at same index, or move with a |
390 |
> |
* power of two offset. We eliminate unnecessary node |
391 |
> |
* creation by catching cases where old nodes can be |
392 |
> |
* reused because their next fields won't change. |
393 |
> |
* Statistically, at the default threshold, only about |
394 |
> |
* one-sixth of them need cloning when a table |
395 |
> |
* doubles. The nodes they replace will be garbage |
396 |
> |
* collectable as soon as they are no longer referenced by |
397 |
> |
* any reader thread that may be in the midst of |
398 |
> |
* concurrently traversing table. Entry accesses use plain |
399 |
> |
* array indexing because they are followed by volatile |
400 |
> |
* table write. |
401 |
|
*/ |
402 |
< |
|
403 |
< |
HashEntry[] newTable = new HashEntry[oldCapacity << 1]; |
404 |
< |
threshold = (int)(newTable.length * loadFactor); |
405 |
< |
int sizeMask = newTable.length - 1; |
402 |
> |
HashEntry<K,V>[] oldTable = table; |
403 |
> |
int oldCapacity = oldTable.length; |
404 |
> |
int newCapacity = oldCapacity << 1; |
405 |
> |
threshold = (int)(newCapacity * loadFactor); |
406 |
> |
HashEntry<K,V>[] newTable = |
407 |
> |
(HashEntry<K,V>[]) new HashEntry<?,?>[newCapacity]; |
408 |
> |
int sizeMask = newCapacity - 1; |
409 |
|
for (int i = 0; i < oldCapacity ; i++) { |
410 |
< |
// We need to guarantee that any existing reads of old Map can |
428 |
< |
// proceed. So we cannot yet null out each bin. |
429 |
< |
HashEntry<K,V> e = (HashEntry<K,V>)oldTable[i]; |
430 |
< |
|
410 |
> |
HashEntry<K,V> e = oldTable[i]; |
411 |
|
if (e != null) { |
412 |
|
HashEntry<K,V> next = e.next; |
413 |
|
int idx = e.hash & sizeMask; |
414 |
< |
|
435 |
< |
// Single node on list |
436 |
< |
if (next == null) |
414 |
> |
if (next == null) // Single node on list |
415 |
|
newTable[idx] = e; |
416 |
< |
|
439 |
< |
else { |
440 |
< |
// Reuse trailing consecutive sequence at same slot |
416 |
> |
else { // Reuse consecutive sequence at same slot |
417 |
|
HashEntry<K,V> lastRun = e; |
418 |
|
int lastIdx = idx; |
419 |
|
for (HashEntry<K,V> last = next; |
426 |
|
} |
427 |
|
} |
428 |
|
newTable[lastIdx] = lastRun; |
429 |
< |
|
454 |
< |
// Clone all remaining nodes |
429 |
> |
// Clone remaining nodes |
430 |
|
for (HashEntry<K,V> p = e; p != lastRun; p = p.next) { |
431 |
< |
int k = p.hash & sizeMask; |
432 |
< |
HashEntry<K,V> n = (HashEntry<K,V>)newTable[k]; |
433 |
< |
newTable[k] = new HashEntry<K,V>(p.key, p.hash, |
434 |
< |
n, p.value); |
431 |
> |
V v = p.value; |
432 |
> |
int h = p.hash; |
433 |
> |
int k = h & sizeMask; |
434 |
> |
HashEntry<K,V> n = newTable[k]; |
435 |
> |
newTable[k] = new HashEntry<K,V>(h, p.key, v, n); |
436 |
|
} |
437 |
|
} |
438 |
|
} |
439 |
|
} |
440 |
+ |
int nodeIndex = node.hash & sizeMask; // add the new node |
441 |
+ |
node.setNext(newTable[nodeIndex]); |
442 |
+ |
newTable[nodeIndex] = node; |
443 |
|
table = newTable; |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
+ |
* Scans for a node containing given key while trying to |
448 |
+ |
* acquire lock, creating and returning one if not found. Upon |
449 |
+ |
* return, guarantees that lock is held. Unlike in most |
450 |
+ |
* methods, calls to method equals are not screened: Since |
451 |
+ |
* traversal speed doesn't matter, we might as well help warm |
452 |
+ |
* up the associated code and accesses as well. |
453 |
+ |
* |
454 |
+ |
* @return a new node if key not found, else null |
455 |
+ |
*/ |
456 |
+ |
private HashEntry<K,V> scanAndLockForPut(K key, int hash, V value) { |
457 |
+ |
HashEntry<K,V> first = entryForHash(this, hash); |
458 |
+ |
HashEntry<K,V> e = first; |
459 |
+ |
HashEntry<K,V> node = null; |
460 |
+ |
int retries = -1; // negative while locating node |
461 |
+ |
while (!tryLock()) { |
462 |
+ |
HashEntry<K,V> f; // to recheck first below |
463 |
+ |
if (retries < 0) { |
464 |
+ |
if (e == null) { |
465 |
+ |
if (node == null) // speculatively create node |
466 |
+ |
node = new HashEntry<K,V>(hash, key, value, null); |
467 |
+ |
retries = 0; |
468 |
+ |
} |
469 |
+ |
else if (key.equals(e.key)) |
470 |
+ |
retries = 0; |
471 |
+ |
else |
472 |
+ |
e = e.next; |
473 |
+ |
} |
474 |
+ |
else if (++retries > MAX_SCAN_RETRIES) { |
475 |
+ |
lock(); |
476 |
+ |
break; |
477 |
+ |
} |
478 |
+ |
else if ((retries & 1) == 0 && |
479 |
+ |
(f = entryForHash(this, hash)) != first) { |
480 |
+ |
e = first = f; // re-traverse if entry changed |
481 |
+ |
retries = -1; |
482 |
+ |
} |
483 |
+ |
} |
484 |
+ |
return node; |
485 |
+ |
} |
486 |
+ |
|
487 |
+ |
/** |
488 |
+ |
* Scans for a node containing the given key while trying to |
489 |
+ |
* acquire lock for a remove or replace operation. Upon |
490 |
+ |
* return, guarantees that lock is held. Note that we must |
491 |
+ |
* lock even if the key is not found, to ensure sequential |
492 |
+ |
* consistency of updates. |
493 |
+ |
*/ |
494 |
+ |
private void scanAndLock(Object key, int hash) { |
495 |
+ |
// similar to but simpler than scanAndLockForPut |
496 |
+ |
HashEntry<K,V> first = entryForHash(this, hash); |
497 |
+ |
HashEntry<K,V> e = first; |
498 |
+ |
int retries = -1; |
499 |
+ |
while (!tryLock()) { |
500 |
+ |
HashEntry<K,V> f; |
501 |
+ |
if (retries < 0) { |
502 |
+ |
if (e == null || key.equals(e.key)) |
503 |
+ |
retries = 0; |
504 |
+ |
else |
505 |
+ |
e = e.next; |
506 |
+ |
} |
507 |
+ |
else if (++retries > MAX_SCAN_RETRIES) { |
508 |
+ |
lock(); |
509 |
+ |
break; |
510 |
+ |
} |
511 |
+ |
else if ((retries & 1) == 0 && |
512 |
+ |
(f = entryForHash(this, hash)) != first) { |
513 |
+ |
e = first = f; |
514 |
+ |
retries = -1; |
515 |
+ |
} |
516 |
+ |
} |
517 |
+ |
} |
518 |
+ |
|
519 |
+ |
/** |
520 |
|
* Remove; match on key only if value null, else match both. |
521 |
|
*/ |
522 |
< |
V remove(Object key, int hash, Object value) { |
523 |
< |
lock(); |
522 |
> |
final V remove(Object key, int hash, Object value) { |
523 |
> |
if (!tryLock()) |
524 |
> |
scanAndLock(key, hash); |
525 |
> |
V oldValue = null; |
526 |
|
try { |
527 |
< |
int c = count - 1; |
528 |
< |
HashEntry[] tab = table; |
529 |
< |
int index = hash & (tab.length - 1); |
530 |
< |
HashEntry<K,V> first = (HashEntry<K,V>)tab[index]; |
531 |
< |
HashEntry<K,V> e = first; |
532 |
< |
while (e != null && (e.hash != hash || !key.equals(e.key))) |
533 |
< |
e = e.next; |
527 |
> |
HashEntry<K,V>[] tab = table; |
528 |
> |
int index = (tab.length - 1) & hash; |
529 |
> |
HashEntry<K,V> e = entryAt(tab, index); |
530 |
> |
HashEntry<K,V> pred = null; |
531 |
> |
while (e != null) { |
532 |
> |
K k; |
533 |
> |
HashEntry<K,V> next = e.next; |
534 |
> |
if ((k = e.key) == key || |
535 |
> |
(e.hash == hash && key.equals(k))) { |
536 |
> |
V v = e.value; |
537 |
> |
if (value == null || value == v || value.equals(v)) { |
538 |
> |
if (pred == null) |
539 |
> |
setEntryAt(tab, index, next); |
540 |
> |
else |
541 |
> |
pred.setNext(next); |
542 |
> |
++modCount; |
543 |
> |
--count; |
544 |
> |
oldValue = v; |
545 |
> |
} |
546 |
> |
break; |
547 |
> |
} |
548 |
> |
pred = e; |
549 |
> |
e = next; |
550 |
> |
} |
551 |
> |
} finally { |
552 |
> |
unlock(); |
553 |
> |
} |
554 |
> |
return oldValue; |
555 |
> |
} |
556 |
|
|
557 |
< |
V oldValue = null; |
558 |
< |
if (e != null) { |
559 |
< |
V v = e.value; |
560 |
< |
if (value == null || value.equals(v)) { |
561 |
< |
oldValue = v; |
562 |
< |
// All entries following removed node can stay |
563 |
< |
// in list, but all preceding ones need to be |
564 |
< |
// cloned. |
565 |
< |
++modCount; |
566 |
< |
HashEntry<K,V> newFirst = e.next; |
567 |
< |
for (HashEntry<K,V> p = first; p != e; p = p.next) |
568 |
< |
newFirst = new HashEntry<K,V>(p.key, p.hash, |
569 |
< |
newFirst, p.value); |
570 |
< |
tab[index] = newFirst; |
571 |
< |
count = c; // write-volatile |
557 |
> |
final boolean replace(K key, int hash, V oldValue, V newValue) { |
558 |
> |
if (!tryLock()) |
559 |
> |
scanAndLock(key, hash); |
560 |
> |
boolean replaced = false; |
561 |
> |
try { |
562 |
> |
HashEntry<K,V> e; |
563 |
> |
for (e = entryForHash(this, hash); e != null; e = e.next) { |
564 |
> |
K k; |
565 |
> |
if ((k = e.key) == key || |
566 |
> |
(e.hash == hash && key.equals(k))) { |
567 |
> |
if (oldValue.equals(e.value)) { |
568 |
> |
e.value = newValue; |
569 |
> |
++modCount; |
570 |
> |
replaced = true; |
571 |
> |
} |
572 |
> |
break; |
573 |
|
} |
574 |
|
} |
498 |
– |
return oldValue; |
575 |
|
} finally { |
576 |
|
unlock(); |
577 |
|
} |
578 |
+ |
return replaced; |
579 |
|
} |
580 |
|
|
581 |
< |
void clear() { |
582 |
< |
if (count != 0) { |
583 |
< |
lock(); |
584 |
< |
try { |
585 |
< |
HashEntry[] tab = table; |
586 |
< |
for (int i = 0; i < tab.length ; i++) |
587 |
< |
tab[i] = null; |
588 |
< |
++modCount; |
589 |
< |
count = 0; // write-volatile |
590 |
< |
} finally { |
591 |
< |
unlock(); |
581 |
> |
final V replace(K key, int hash, V value) { |
582 |
> |
if (!tryLock()) |
583 |
> |
scanAndLock(key, hash); |
584 |
> |
V oldValue = null; |
585 |
> |
try { |
586 |
> |
HashEntry<K,V> e; |
587 |
> |
for (e = entryForHash(this, hash); e != null; e = e.next) { |
588 |
> |
K k; |
589 |
> |
if ((k = e.key) == key || |
590 |
> |
(e.hash == hash && key.equals(k))) { |
591 |
> |
oldValue = e.value; |
592 |
> |
e.value = value; |
593 |
> |
++modCount; |
594 |
> |
break; |
595 |
> |
} |
596 |
|
} |
597 |
+ |
} finally { |
598 |
+ |
unlock(); |
599 |
+ |
} |
600 |
+ |
return oldValue; |
601 |
+ |
} |
602 |
+ |
|
603 |
+ |
final void clear() { |
604 |
+ |
lock(); |
605 |
+ |
try { |
606 |
+ |
HashEntry<K,V>[] tab = table; |
607 |
+ |
for (int i = 0; i < tab.length ; i++) |
608 |
+ |
setEntryAt(tab, i, null); |
609 |
+ |
++modCount; |
610 |
+ |
count = 0; |
611 |
+ |
} finally { |
612 |
+ |
unlock(); |
613 |
|
} |
614 |
|
} |
615 |
|
} |
616 |
|
|
617 |
+ |
// Accessing segments |
618 |
+ |
|
619 |
|
/** |
620 |
< |
* ConcurrentHashMap list entry. Note that this is never exported |
621 |
< |
* out as a user-visible Map.Entry |
620 |
> |
* Gets the jth element of given segment array (if nonnull) with |
621 |
> |
* volatile element access semantics via Unsafe. (The null check |
622 |
> |
* can trigger harmlessly only during deserialization.) Note: |
623 |
> |
* because each element of segments array is set only once (using |
624 |
> |
* fully ordered writes), some performance-sensitive methods rely |
625 |
> |
* on this method only as a recheck upon null reads. |
626 |
|
*/ |
627 |
< |
static final class HashEntry<K,V> { |
628 |
< |
final K key; |
629 |
< |
final int hash; |
630 |
< |
volatile V value; |
631 |
< |
final HashEntry<K,V> next; |
627 |
> |
@SuppressWarnings("unchecked") |
628 |
> |
static final <K,V> Segment<K,V> segmentAt(Segment<K,V>[] ss, int j) { |
629 |
> |
long u = (j << SSHIFT) + SBASE; |
630 |
> |
return ss == null ? null : |
631 |
> |
(Segment<K,V>) UNSAFE.getObjectVolatile(ss, u); |
632 |
> |
} |
633 |
|
|
634 |
< |
HashEntry(K key, int hash, HashEntry<K,V> next, V value) { |
635 |
< |
this.key = key; |
636 |
< |
this.hash = hash; |
637 |
< |
this.next = next; |
638 |
< |
this.value = value; |
634 |
> |
/** |
635 |
> |
* Returns the segment for the given index, creating it and |
636 |
> |
* recording in segment table (via CAS) if not already present. |
637 |
> |
* |
638 |
> |
* @param k the index |
639 |
> |
* @return the segment |
640 |
> |
*/ |
641 |
> |
@SuppressWarnings("unchecked") |
642 |
> |
private Segment<K,V> ensureSegment(int k) { |
643 |
> |
final Segment<K,V>[] ss = this.segments; |
644 |
> |
long u = (k << SSHIFT) + SBASE; // raw offset |
645 |
> |
Segment<K,V> seg; |
646 |
> |
if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) == null) { |
647 |
> |
Segment<K,V> proto = ss[0]; // use segment 0 as prototype |
648 |
> |
int cap = proto.table.length; |
649 |
> |
float lf = proto.loadFactor; |
650 |
> |
int threshold = (int)(cap * lf); |
651 |
> |
HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry<?,?>[cap]; |
652 |
> |
if ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) |
653 |
> |
== null) { // recheck |
654 |
> |
Segment<K,V> s = new Segment<K,V>(lf, threshold, tab); |
655 |
> |
while ((seg = (Segment<K,V>)UNSAFE.getObjectVolatile(ss, u)) |
656 |
> |
== null) { |
657 |
> |
if (UNSAFE.compareAndSwapObject(ss, u, null, seg = s)) |
658 |
> |
break; |
659 |
> |
} |
660 |
> |
} |
661 |
|
} |
662 |
+ |
return seg; |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
// Hash-based segment and entry accesses |
666 |
+ |
|
667 |
+ |
/** |
668 |
+ |
* Gets the segment for the given hash code. |
669 |
+ |
*/ |
670 |
+ |
@SuppressWarnings("unchecked") |
671 |
+ |
private Segment<K,V> segmentForHash(int h) { |
672 |
+ |
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE; |
673 |
+ |
return (Segment<K,V>) UNSAFE.getObjectVolatile(segments, u); |
674 |
|
} |
675 |
|
|
676 |
+ |
/** |
677 |
+ |
* Gets the table entry for the given segment and hash code. |
678 |
+ |
*/ |
679 |
+ |
@SuppressWarnings("unchecked") |
680 |
+ |
static final <K,V> HashEntry<K,V> entryForHash(Segment<K,V> seg, int h) { |
681 |
+ |
HashEntry<K,V>[] tab; |
682 |
+ |
return (seg == null || (tab = seg.table) == null) ? null : |
683 |
+ |
(HashEntry<K,V>) UNSAFE.getObjectVolatile |
684 |
+ |
(tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE); |
685 |
+ |
} |
686 |
|
|
687 |
|
/* ---------------- Public operations -------------- */ |
688 |
|
|
689 |
|
/** |
690 |
|
* Creates a new, empty map with the specified initial |
691 |
< |
* capacity and the specified load factor. |
691 |
> |
* capacity, load factor and concurrency level. |
692 |
|
* |
693 |
|
* @param initialCapacity the initial capacity. The implementation |
694 |
|
* performs internal sizing to accommodate this many elements. |
695 |
|
* @param loadFactor the load factor threshold, used to control resizing. |
696 |
+ |
* Resizing may be performed when the average number of elements per |
697 |
+ |
* bin exceeds this threshold. |
698 |
|
* @param concurrencyLevel the estimated number of concurrently |
699 |
|
* updating threads. The implementation performs internal sizing |
700 |
< |
* to try to accommodate this many threads. |
700 |
> |
* to try to accommodate this many threads. |
701 |
|
* @throws IllegalArgumentException if the initial capacity is |
702 |
|
* negative or the load factor or concurrencyLevel are |
703 |
|
* nonpositive. |
704 |
|
*/ |
705 |
+ |
@SuppressWarnings("unchecked") |
706 |
|
public ConcurrentHashMap(int initialCapacity, |
707 |
|
float loadFactor, int concurrencyLevel) { |
708 |
|
if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0) |
709 |
|
throw new IllegalArgumentException(); |
559 |
– |
|
710 |
|
if (concurrencyLevel > MAX_SEGMENTS) |
711 |
|
concurrencyLevel = MAX_SEGMENTS; |
562 |
– |
|
712 |
|
// Find power-of-two sizes best matching arguments |
713 |
|
int sshift = 0; |
714 |
|
int ssize = 1; |
716 |
|
++sshift; |
717 |
|
ssize <<= 1; |
718 |
|
} |
719 |
< |
segmentShift = 32 - sshift; |
720 |
< |
segmentMask = ssize - 1; |
572 |
< |
this.segments = new Segment[ssize]; |
573 |
< |
|
719 |
> |
this.segmentShift = 32 - sshift; |
720 |
> |
this.segmentMask = ssize - 1; |
721 |
|
if (initialCapacity > MAXIMUM_CAPACITY) |
722 |
|
initialCapacity = MAXIMUM_CAPACITY; |
723 |
|
int c = initialCapacity / ssize; |
724 |
|
if (c * ssize < initialCapacity) |
725 |
|
++c; |
726 |
< |
int cap = 1; |
726 |
> |
int cap = MIN_SEGMENT_TABLE_CAPACITY; |
727 |
|
while (cap < c) |
728 |
|
cap <<= 1; |
729 |
< |
|
730 |
< |
for (int i = 0; i < this.segments.length; ++i) |
731 |
< |
this.segments[i] = new Segment<K,V>(cap, loadFactor); |
729 |
> |
// create segments and segments[0] |
730 |
> |
Segment<K,V> s0 = |
731 |
> |
new Segment<K,V>(loadFactor, (int)(cap * loadFactor), |
732 |
> |
(HashEntry<K,V>[])new HashEntry<?,?>[cap]); |
733 |
> |
Segment<K,V>[] ss = (Segment<K,V>[])new Segment<?,?>[ssize]; |
734 |
> |
UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0] |
735 |
> |
this.segments = ss; |
736 |
|
} |
737 |
|
|
738 |
|
/** |
739 |
< |
* Creates a new, empty map with the specified initial |
740 |
< |
* capacity, and with default load factor and concurrencyLevel. |
739 |
> |
* Creates a new, empty map with the specified initial capacity |
740 |
> |
* and load factor and with the default concurrencyLevel (16). |
741 |
|
* |
742 |
|
* @param initialCapacity The implementation performs internal |
743 |
|
* sizing to accommodate this many elements. |
744 |
+ |
* @param loadFactor the load factor threshold, used to control resizing. |
745 |
+ |
* Resizing may be performed when the average number of elements per |
746 |
+ |
* bin exceeds this threshold. |
747 |
+ |
* @throws IllegalArgumentException if the initial capacity of |
748 |
+ |
* elements is negative or the load factor is nonpositive |
749 |
+ |
* |
750 |
+ |
* @since 1.6 |
751 |
+ |
*/ |
752 |
+ |
public ConcurrentHashMap(int initialCapacity, float loadFactor) { |
753 |
+ |
this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL); |
754 |
+ |
} |
755 |
+ |
|
756 |
+ |
/** |
757 |
+ |
* Creates a new, empty map with the specified initial capacity, |
758 |
+ |
* and with default load factor (0.75) and concurrencyLevel (16). |
759 |
+ |
* |
760 |
+ |
* @param initialCapacity the initial capacity. The implementation |
761 |
+ |
* performs internal sizing to accommodate this many elements. |
762 |
|
* @throws IllegalArgumentException if the initial capacity of |
763 |
|
* elements is negative. |
764 |
|
*/ |
765 |
|
public ConcurrentHashMap(int initialCapacity) { |
766 |
< |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
766 |
> |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
767 |
|
} |
768 |
|
|
769 |
|
/** |
770 |
< |
* Creates a new, empty map with a default initial capacity, |
771 |
< |
* load factor, and concurrencyLevel. |
770 |
> |
* Creates a new, empty map with a default initial capacity (16), |
771 |
> |
* load factor (0.75) and concurrencyLevel (16). |
772 |
|
*/ |
773 |
|
public ConcurrentHashMap() { |
774 |
< |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
774 |
> |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
775 |
|
} |
776 |
|
|
777 |
|
/** |
778 |
< |
* Creates a new map with the same mappings as the given map. The |
779 |
< |
* map is created with a capacity of twice the number of mappings in |
780 |
< |
* the given map or 11 (whichever is greater), and a default load factor. |
781 |
< |
* @param t the map |
778 |
> |
* Creates a new map with the same mappings as the given map. |
779 |
> |
* The map is created with a capacity of 1.5 times the number |
780 |
> |
* of mappings in the given map or 16 (whichever is greater), |
781 |
> |
* and a default load factor (0.75) and concurrencyLevel (16). |
782 |
> |
* |
783 |
> |
* @param m the map |
784 |
|
*/ |
785 |
< |
public ConcurrentHashMap(Map<? extends K, ? extends V> t) { |
786 |
< |
this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1, |
787 |
< |
11), |
788 |
< |
DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
789 |
< |
putAll(t); |
785 |
> |
public ConcurrentHashMap(Map<? extends K, ? extends V> m) { |
786 |
> |
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, |
787 |
> |
DEFAULT_INITIAL_CAPACITY), |
788 |
> |
DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL); |
789 |
> |
putAll(m); |
790 |
|
} |
791 |
|
|
792 |
< |
// inherit Map javadoc |
792 |
> |
/** |
793 |
> |
* Returns <tt>true</tt> if this map contains no key-value mappings. |
794 |
> |
* |
795 |
> |
* @return <tt>true</tt> if this map contains no key-value mappings |
796 |
> |
*/ |
797 |
|
public boolean isEmpty() { |
623 |
– |
final Segment[] segments = this.segments; |
798 |
|
/* |
799 |
< |
* We keep track of per-segment modCounts to avoid ABA |
800 |
< |
* problems in which an element in one segment was added and |
801 |
< |
* in another removed during traversal, in which case the |
802 |
< |
* table was never actually empty at any point. Note the |
803 |
< |
* similar use of modCounts in the size() and containsValue() |
804 |
< |
* methods, which are the only other methods also susceptible |
805 |
< |
* to ABA problems. |
799 |
> |
* Sum per-segment modCounts to avoid mis-reporting when |
800 |
> |
* elements are concurrently added and removed in one segment |
801 |
> |
* while checking another, in which case the table was never |
802 |
> |
* actually empty at any point. (The sum ensures accuracy up |
803 |
> |
* through at least 1<<31 per-segment modifications before |
804 |
> |
* recheck.) Methods size() and containsValue() use similar |
805 |
> |
* constructions for stability checks. |
806 |
|
*/ |
807 |
< |
int[] mc = new int[segments.length]; |
808 |
< |
int mcsum = 0; |
809 |
< |
for (int i = 0; i < segments.length; ++i) { |
810 |
< |
if (segments[i].count != 0) |
811 |
< |
return false; |
812 |
< |
else |
639 |
< |
mcsum += mc[i] = segments[i].modCount; |
640 |
< |
} |
641 |
< |
// If mcsum happens to be zero, then we know we got a snapshot |
642 |
< |
// before any modifications at all were made. This is |
643 |
< |
// probably common enough to bother tracking. |
644 |
< |
if (mcsum != 0) { |
645 |
< |
for (int i = 0; i < segments.length; ++i) { |
646 |
< |
if (segments[i].count != 0 || |
647 |
< |
mc[i] != segments[i].modCount) |
807 |
> |
long sum = 0L; |
808 |
> |
final Segment<K,V>[] segments = this.segments; |
809 |
> |
for (int j = 0; j < segments.length; ++j) { |
810 |
> |
Segment<K,V> seg = segmentAt(segments, j); |
811 |
> |
if (seg != null) { |
812 |
> |
if (seg.count != 0) |
813 |
|
return false; |
814 |
+ |
sum += seg.modCount; |
815 |
+ |
} |
816 |
+ |
} |
817 |
+ |
if (sum != 0L) { // recheck unless no modifications |
818 |
+ |
for (int j = 0; j < segments.length; ++j) { |
819 |
+ |
Segment<K,V> seg = segmentAt(segments, j); |
820 |
+ |
if (seg != null) { |
821 |
+ |
if (seg.count != 0) |
822 |
+ |
return false; |
823 |
+ |
sum -= seg.modCount; |
824 |
+ |
} |
825 |
|
} |
826 |
+ |
if (sum != 0L) |
827 |
+ |
return false; |
828 |
|
} |
829 |
|
return true; |
830 |
|
} |
831 |
|
|
832 |
< |
// inherit Map javadoc |
832 |
> |
/** |
833 |
> |
* Returns the number of key-value mappings in this map. If the |
834 |
> |
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns |
835 |
> |
* <tt>Integer.MAX_VALUE</tt>. |
836 |
> |
* |
837 |
> |
* @return the number of key-value mappings in this map |
838 |
> |
*/ |
839 |
|
public int size() { |
840 |
< |
final Segment[] segments = this.segments; |
657 |
< |
long sum = 0; |
658 |
< |
long check = 0; |
659 |
< |
int[] mc = new int[segments.length]; |
660 |
< |
// Try at most twice to get accurate count. On failure due to |
840 |
> |
// Try a few times to get accurate count. On failure due to |
841 |
|
// continuous async changes in table, resort to locking. |
842 |
< |
for (int k = 0; k < 2; ++k) { |
843 |
< |
check = 0; |
844 |
< |
sum = 0; |
845 |
< |
int mcsum = 0; |
846 |
< |
for (int i = 0; i < segments.length; ++i) { |
847 |
< |
sum += segments[i].count; |
848 |
< |
mcsum += mc[i] = segments[i].modCount; |
849 |
< |
} |
850 |
< |
if (mcsum != 0) { |
851 |
< |
for (int i = 0; i < segments.length; ++i) { |
852 |
< |
check += segments[i].count; |
853 |
< |
if (mc[i] != segments[i].modCount) { |
674 |
< |
check = -1; // force retry |
675 |
< |
break; |
676 |
< |
} |
842 |
> |
final Segment<K,V>[] segments = this.segments; |
843 |
> |
final int segmentCount = segments.length; |
844 |
> |
|
845 |
> |
long previousSum = 0L; |
846 |
> |
for (int retries = -1; retries < RETRIES_BEFORE_LOCK; retries++) { |
847 |
> |
long sum = 0L; // sum of modCounts |
848 |
> |
long size = 0L; |
849 |
> |
for (int i = 0; i < segmentCount; i++) { |
850 |
> |
Segment<K,V> segment = segmentAt(segments, i); |
851 |
> |
if (segment != null) { |
852 |
> |
sum += segment.modCount; |
853 |
> |
size += segment.count; |
854 |
|
} |
855 |
|
} |
856 |
< |
if (check == sum) |
857 |
< |
break; |
856 |
> |
if (sum == previousSum) |
857 |
> |
return ((size >>> 31) == 0) ? (int) size : Integer.MAX_VALUE; |
858 |
> |
previousSum = sum; |
859 |
|
} |
682 |
– |
if (check != sum) { // Resort to locking all segments |
683 |
– |
sum = 0; |
684 |
– |
for (int i = 0; i < segments.length; ++i) |
685 |
– |
segments[i].lock(); |
686 |
– |
for (int i = 0; i < segments.length; ++i) |
687 |
– |
sum += segments[i].count; |
688 |
– |
for (int i = 0; i < segments.length; ++i) |
689 |
– |
segments[i].unlock(); |
690 |
– |
} |
691 |
– |
if (sum > Integer.MAX_VALUE) |
692 |
– |
return Integer.MAX_VALUE; |
693 |
– |
else |
694 |
– |
return (int)sum; |
695 |
– |
} |
860 |
|
|
861 |
+ |
long size = 0L; |
862 |
+ |
for (int i = 0; i < segmentCount; i++) { |
863 |
+ |
Segment<K,V> segment = ensureSegment(i); |
864 |
+ |
segment.lock(); |
865 |
+ |
size += segment.count; |
866 |
+ |
} |
867 |
+ |
for (int i = 0; i < segmentCount; i++) |
868 |
+ |
segments[i].unlock(); |
869 |
+ |
return ((size >>> 31) == 0) ? (int) size : Integer.MAX_VALUE; |
870 |
+ |
} |
871 |
|
|
872 |
|
/** |
873 |
< |
* Returns the value to which the specified key is mapped in this table. |
874 |
< |
* |
875 |
< |
* @param key a key in the table. |
876 |
< |
* @return the value to which the key is mapped in this table; |
877 |
< |
* <tt>null</tt> if the key is not mapped to any value in |
878 |
< |
* this table. |
879 |
< |
* @throws NullPointerException if the key is |
880 |
< |
* <tt>null</tt>. |
873 |
> |
* Returns the value to which the specified key is mapped, |
874 |
> |
* or {@code null} if this map contains no mapping for the key. |
875 |
> |
* |
876 |
> |
* <p>More formally, if this map contains a mapping from a key |
877 |
> |
* {@code k} to a value {@code v} such that {@code key.equals(k)}, |
878 |
> |
* then this method returns {@code v}; otherwise it returns |
879 |
> |
* {@code null}. (There can be at most one such mapping.) |
880 |
> |
* |
881 |
> |
* @throws NullPointerException if the specified key is null |
882 |
|
*/ |
883 |
|
public V get(Object key) { |
884 |
< |
int hash = hash(key); // throws NullPointerException if key null |
885 |
< |
return segmentFor(hash).get(key, hash); |
884 |
> |
Segment<K,V> s; // manually integrate access methods to reduce overhead |
885 |
> |
HashEntry<K,V>[] tab; |
886 |
> |
int h = hash(key.hashCode()); |
887 |
> |
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE; |
888 |
> |
if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null && |
889 |
> |
(tab = s.table) != null) { |
890 |
> |
for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile |
891 |
> |
(tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE); |
892 |
> |
e != null; e = e.next) { |
893 |
> |
K k; |
894 |
> |
if ((k = e.key) == key || (e.hash == h && key.equals(k))) |
895 |
> |
return e.value; |
896 |
> |
} |
897 |
> |
} |
898 |
> |
return null; |
899 |
|
} |
900 |
|
|
901 |
|
/** |
902 |
|
* Tests if the specified object is a key in this table. |
903 |
|
* |
904 |
< |
* @param key possible key. |
905 |
< |
* @return <tt>true</tt> if and only if the specified object |
906 |
< |
* is a key in this table, as determined by the |
907 |
< |
* <tt>equals</tt> method; <tt>false</tt> otherwise. |
908 |
< |
* @throws NullPointerException if the key is |
721 |
< |
* <tt>null</tt>. |
904 |
> |
* @param key possible key |
905 |
> |
* @return <tt>true</tt> if and only if the specified object |
906 |
> |
* is a key in this table, as determined by the |
907 |
> |
* <tt>equals</tt> method; <tt>false</tt> otherwise. |
908 |
> |
* @throws NullPointerException if the specified key is null |
909 |
|
*/ |
910 |
+ |
@SuppressWarnings("unchecked") |
911 |
|
public boolean containsKey(Object key) { |
912 |
< |
int hash = hash(key); // throws NullPointerException if key null |
913 |
< |
return segmentFor(hash).containsKey(key, hash); |
912 |
> |
Segment<K,V> s; // same as get() except no need for volatile value read |
913 |
> |
HashEntry<K,V>[] tab; |
914 |
> |
int h = hash(key.hashCode()); |
915 |
> |
long u = (((h >>> segmentShift) & segmentMask) << SSHIFT) + SBASE; |
916 |
> |
if ((s = (Segment<K,V>)UNSAFE.getObjectVolatile(segments, u)) != null && |
917 |
> |
(tab = s.table) != null) { |
918 |
> |
for (HashEntry<K,V> e = (HashEntry<K,V>) UNSAFE.getObjectVolatile |
919 |
> |
(tab, ((long)(((tab.length - 1) & h)) << TSHIFT) + TBASE); |
920 |
> |
e != null; e = e.next) { |
921 |
> |
K k; |
922 |
> |
if ((k = e.key) == key || (e.hash == h && key.equals(k))) |
923 |
> |
return true; |
924 |
> |
} |
925 |
> |
} |
926 |
> |
return false; |
927 |
|
} |
928 |
|
|
929 |
|
/** |
932 |
|
* traversal of the hash table, and so is much slower than |
933 |
|
* method <tt>containsKey</tt>. |
934 |
|
* |
935 |
< |
* @param value value whose presence in this map is to be tested. |
935 |
> |
* @param value value whose presence in this map is to be tested |
936 |
|
* @return <tt>true</tt> if this map maps one or more keys to the |
937 |
< |
* specified value. |
938 |
< |
* @throws NullPointerException if the value is <tt>null</tt>. |
937 |
> |
* specified value |
938 |
> |
* @throws NullPointerException if the specified value is null |
939 |
|
*/ |
940 |
|
public boolean containsValue(Object value) { |
941 |
+ |
// Same idea as size() |
942 |
|
if (value == null) |
943 |
|
throw new NullPointerException(); |
944 |
< |
|
945 |
< |
// See explanation of modCount use above |
946 |
< |
|
745 |
< |
final Segment[] segments = this.segments; |
746 |
< |
int[] mc = new int[segments.length]; |
747 |
< |
|
748 |
< |
// Try at most twice without locking |
749 |
< |
for (int k = 0; k < 2; ++k) { |
750 |
< |
int sum = 0; |
751 |
< |
int mcsum = 0; |
752 |
< |
for (int i = 0; i < segments.length; ++i) { |
753 |
< |
int c = segments[i].count; |
754 |
< |
mcsum += mc[i] = segments[i].modCount; |
755 |
< |
if (segments[i].containsValue(value)) |
756 |
< |
return true; |
757 |
< |
} |
758 |
< |
boolean cleanSweep = true; |
759 |
< |
if (mcsum != 0) { |
760 |
< |
for (int i = 0; i < segments.length; ++i) { |
761 |
< |
int c = segments[i].count; |
762 |
< |
if (mc[i] != segments[i].modCount) { |
763 |
< |
cleanSweep = false; |
764 |
< |
break; |
765 |
< |
} |
766 |
< |
} |
767 |
< |
} |
768 |
< |
if (cleanSweep) |
769 |
< |
return false; |
770 |
< |
} |
771 |
< |
// Resort to locking all segments |
772 |
< |
for (int i = 0; i < segments.length; ++i) |
773 |
< |
segments[i].lock(); |
774 |
< |
boolean found = false; |
944 |
> |
final Segment<K,V>[] segments = this.segments; |
945 |
> |
long previousSum = 0L; |
946 |
> |
int lockCount = 0; |
947 |
|
try { |
948 |
< |
for (int i = 0; i < segments.length; ++i) { |
949 |
< |
if (segments[i].containsValue(value)) { |
950 |
< |
found = true; |
951 |
< |
break; |
948 |
> |
for (int retries = -1; ; retries++) { |
949 |
> |
long sum = 0L; // sum of modCounts |
950 |
> |
for (int j = 0; j < segments.length; j++) { |
951 |
> |
Segment<K,V> segment; |
952 |
> |
if (retries == RETRIES_BEFORE_LOCK) { |
953 |
> |
segment = ensureSegment(j); |
954 |
> |
segment.lock(); |
955 |
> |
lockCount++; |
956 |
> |
} else { |
957 |
> |
segment = segmentAt(segments, j); |
958 |
> |
if (segment == null) |
959 |
> |
continue; |
960 |
> |
} |
961 |
> |
HashEntry<K,V>[] tab = segment.table; |
962 |
> |
if (tab != null) { |
963 |
> |
for (int i = 0 ; i < tab.length; i++) { |
964 |
> |
HashEntry<K,V> e; |
965 |
> |
for (e = entryAt(tab, i); e != null; e = e.next) { |
966 |
> |
V v = e.value; |
967 |
> |
if (v != null && value.equals(v)) |
968 |
> |
return true; |
969 |
> |
} |
970 |
> |
} |
971 |
> |
sum += segment.modCount; |
972 |
> |
} |
973 |
|
} |
974 |
+ |
if ((retries >= 0 && sum == previousSum) || lockCount > 0) |
975 |
+ |
return false; |
976 |
+ |
previousSum = sum; |
977 |
|
} |
978 |
|
} finally { |
979 |
< |
for (int i = 0; i < segments.length; ++i) |
980 |
< |
segments[i].unlock(); |
979 |
> |
for (int j = 0; j < lockCount; j++) |
980 |
> |
segments[j].unlock(); |
981 |
|
} |
786 |
– |
return found; |
982 |
|
} |
983 |
|
|
984 |
|
/** |
985 |
|
* Legacy method testing if some key maps into the specified value |
986 |
|
* in this table. This method is identical in functionality to |
987 |
< |
* {@link #containsValue}, and exists solely to ensure |
987 |
> |
* {@link #containsValue}, and exists solely to ensure |
988 |
|
* full compatibility with class {@link java.util.Hashtable}, |
989 |
|
* which supported this method prior to introduction of the |
990 |
|
* Java Collections framework. |
991 |
< |
|
992 |
< |
* @param value a value to search for. |
993 |
< |
* @return <tt>true</tt> if and only if some key maps to the |
994 |
< |
* <tt>value</tt> argument in this table as |
995 |
< |
* determined by the <tt>equals</tt> method; |
996 |
< |
* <tt>false</tt> otherwise. |
997 |
< |
* @throws NullPointerException if the value is <tt>null</tt>. |
991 |
> |
* |
992 |
> |
* @param value a value to search for |
993 |
> |
* @return <tt>true</tt> if and only if some key maps to the |
994 |
> |
* <tt>value</tt> argument in this table as |
995 |
> |
* determined by the <tt>equals</tt> method; |
996 |
> |
* <tt>false</tt> otherwise |
997 |
> |
* @throws NullPointerException if the specified value is null |
998 |
|
*/ |
999 |
|
public boolean contains(Object value) { |
1000 |
|
return containsValue(value); |
1001 |
|
} |
1002 |
|
|
1003 |
|
/** |
1004 |
< |
* Maps the specified <tt>key</tt> to the specified |
1005 |
< |
* <tt>value</tt> in this table. Neither the key nor the |
811 |
< |
* value can be <tt>null</tt>. |
1004 |
> |
* Maps the specified key to the specified value in this table. |
1005 |
> |
* Neither the key nor the value can be null. |
1006 |
|
* |
1007 |
|
* <p> The value can be retrieved by calling the <tt>get</tt> method |
1008 |
|
* with a key that is equal to the original key. |
1009 |
|
* |
1010 |
< |
* @param key the table key. |
1011 |
< |
* @param value the value. |
1012 |
< |
* @return the previous value of the specified key in this table, |
1013 |
< |
* or <tt>null</tt> if it did not have one. |
1014 |
< |
* @throws NullPointerException if the key or value is |
821 |
< |
* <tt>null</tt>. |
1010 |
> |
* @param key key with which the specified value is to be associated |
1011 |
> |
* @param value value to be associated with the specified key |
1012 |
> |
* @return the previous value associated with <tt>key</tt>, or |
1013 |
> |
* <tt>null</tt> if there was no mapping for <tt>key</tt> |
1014 |
> |
* @throws NullPointerException if the specified key or value is null |
1015 |
|
*/ |
1016 |
+ |
@SuppressWarnings("unchecked") |
1017 |
|
public V put(K key, V value) { |
1018 |
+ |
Segment<K,V> s; |
1019 |
|
if (value == null) |
1020 |
|
throw new NullPointerException(); |
1021 |
< |
int hash = hash(key); |
1022 |
< |
return segmentFor(hash).put(key, hash, value, false); |
1021 |
> |
int hash = hash(key.hashCode()); |
1022 |
> |
int j = (hash >>> segmentShift) & segmentMask; |
1023 |
> |
if ((s = (Segment<K,V>)UNSAFE.getObject // nonvolatile; recheck |
1024 |
> |
(segments, (j << SSHIFT) + SBASE)) == null) // in ensureSegment |
1025 |
> |
s = ensureSegment(j); |
1026 |
> |
return s.put(key, hash, value, false); |
1027 |
|
} |
1028 |
|
|
1029 |
|
/** |
1030 |
< |
* If the specified key is not already associated |
832 |
< |
* with a value, associate it with the given value. |
833 |
< |
* This is equivalent to |
834 |
< |
* <pre> |
835 |
< |
* if (!map.containsKey(key)) |
836 |
< |
* return map.put(key, value); |
837 |
< |
* else |
838 |
< |
* return map.get(key); |
839 |
< |
* </pre> |
840 |
< |
* Except that the action is performed atomically. |
841 |
< |
* @param key key with which the specified value is to be associated. |
842 |
< |
* @param value value to be associated with the specified key. |
843 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
844 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
845 |
< |
* also indicate that the map previously associated <tt>null</tt> |
846 |
< |
* with the specified key, if the implementation supports |
847 |
< |
* <tt>null</tt> values. |
848 |
< |
* |
849 |
< |
* @throws UnsupportedOperationException if the <tt>put</tt> operation is |
850 |
< |
* not supported by this map. |
851 |
< |
* @throws ClassCastException if the class of the specified key or value |
852 |
< |
* prevents it from being stored in this map. |
853 |
< |
* @throws NullPointerException if the specified key or value is |
854 |
< |
* <tt>null</tt>. |
1030 |
> |
* {@inheritDoc} |
1031 |
|
* |
1032 |
< |
**/ |
1032 |
> |
* @return the previous value associated with the specified key, |
1033 |
> |
* or <tt>null</tt> if there was no mapping for the key |
1034 |
> |
* @throws NullPointerException if the specified key or value is null |
1035 |
> |
*/ |
1036 |
> |
@SuppressWarnings("unchecked") |
1037 |
|
public V putIfAbsent(K key, V value) { |
1038 |
+ |
Segment<K,V> s; |
1039 |
|
if (value == null) |
1040 |
|
throw new NullPointerException(); |
1041 |
< |
int hash = hash(key); |
1042 |
< |
return segmentFor(hash).put(key, hash, value, true); |
1041 |
> |
int hash = hash(key.hashCode()); |
1042 |
> |
int j = (hash >>> segmentShift) & segmentMask; |
1043 |
> |
if ((s = (Segment<K,V>)UNSAFE.getObject |
1044 |
> |
(segments, (j << SSHIFT) + SBASE)) == null) |
1045 |
> |
s = ensureSegment(j); |
1046 |
> |
return s.put(key, hash, value, true); |
1047 |
|
} |
1048 |
|
|
864 |
– |
|
1049 |
|
/** |
1050 |
|
* Copies all of the mappings from the specified map to this one. |
867 |
– |
* |
1051 |
|
* These mappings replace any mappings that this map had for any of the |
1052 |
< |
* keys currently in the specified Map. |
1052 |
> |
* keys currently in the specified map. |
1053 |
|
* |
1054 |
< |
* @param t Mappings to be stored in this map. |
1054 |
> |
* @param m mappings to be stored in this map |
1055 |
|
*/ |
1056 |
< |
public void putAll(Map<? extends K, ? extends V> t) { |
1057 |
< |
for (Iterator<Map.Entry<? extends K, ? extends V>> it = (Iterator<Map.Entry<? extends K, ? extends V>>) t.entrySet().iterator(); it.hasNext(); ) { |
875 |
< |
Entry<? extends K, ? extends V> e = it.next(); |
1056 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
1057 |
> |
for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) |
1058 |
|
put(e.getKey(), e.getValue()); |
877 |
– |
} |
1059 |
|
} |
1060 |
|
|
1061 |
|
/** |
1062 |
< |
* Removes the key (and its corresponding value) from this |
1063 |
< |
* table. This method does nothing if the key is not in the table. |
1062 |
> |
* Removes the key (and its corresponding value) from this map. |
1063 |
> |
* This method does nothing if the key is not in the map. |
1064 |
|
* |
1065 |
< |
* @param key the key that needs to be removed. |
1066 |
< |
* @return the value to which the key had been mapped in this table, |
1067 |
< |
* or <tt>null</tt> if the key did not have a mapping. |
1068 |
< |
* @throws NullPointerException if the key is |
888 |
< |
* <tt>null</tt>. |
1065 |
> |
* @param key the key that needs to be removed |
1066 |
> |
* @return the previous value associated with <tt>key</tt>, or |
1067 |
> |
* <tt>null</tt> if there was no mapping for <tt>key</tt> |
1068 |
> |
* @throws NullPointerException if the specified key is null |
1069 |
|
*/ |
1070 |
|
public V remove(Object key) { |
1071 |
< |
int hash = hash(key); |
1072 |
< |
return segmentFor(hash).remove(key, hash, null); |
1071 |
> |
int hash = hash(key.hashCode()); |
1072 |
> |
Segment<K,V> s = segmentForHash(hash); |
1073 |
> |
return s == null ? null : s.remove(key, hash, null); |
1074 |
|
} |
1075 |
|
|
1076 |
|
/** |
1077 |
< |
* Remove entry for key only if currently mapped to given value. |
1078 |
< |
* Acts as |
1079 |
< |
* <pre> |
899 |
< |
* if (map.get(key).equals(value)) { |
900 |
< |
* map.remove(key); |
901 |
< |
* return true; |
902 |
< |
* } else return false; |
903 |
< |
* </pre> |
904 |
< |
* except that the action is performed atomically. |
905 |
< |
* @param key key with which the specified value is associated. |
906 |
< |
* @param value value associated with the specified key. |
907 |
< |
* @return true if the value was removed |
908 |
< |
* @throws NullPointerException if the specified key is |
909 |
< |
* <tt>null</tt>. |
1077 |
> |
* {@inheritDoc} |
1078 |
> |
* |
1079 |
> |
* @throws NullPointerException if the specified key is null |
1080 |
|
*/ |
1081 |
|
public boolean remove(Object key, Object value) { |
1082 |
< |
int hash = hash(key); |
1083 |
< |
return segmentFor(hash).remove(key, hash, value) != null; |
1082 |
> |
int hash = hash(key.hashCode()); |
1083 |
> |
Segment<K,V> s; |
1084 |
> |
return value != null && (s = segmentForHash(hash)) != null && |
1085 |
> |
s.remove(key, hash, value) != null; |
1086 |
|
} |
1087 |
|
|
916 |
– |
|
1088 |
|
/** |
1089 |
< |
* Replace entry for key only if currently mapped to given value. |
1090 |
< |
* Acts as |
1091 |
< |
* <pre> |
921 |
< |
* if (map.get(key).equals(oldValue)) { |
922 |
< |
* map.put(key, newValue); |
923 |
< |
* return true; |
924 |
< |
* } else return false; |
925 |
< |
* </pre> |
926 |
< |
* except that the action is performed atomically. |
927 |
< |
* @param key key with which the specified value is associated. |
928 |
< |
* @param oldValue value expected to be associated with the specified key. |
929 |
< |
* @param newValue value to be associated with the specified key. |
930 |
< |
* @return true if the value was replaced |
931 |
< |
* @throws NullPointerException if the specified key or values are |
932 |
< |
* <tt>null</tt>. |
1089 |
> |
* {@inheritDoc} |
1090 |
> |
* |
1091 |
> |
* @throws NullPointerException if any of the arguments are null |
1092 |
|
*/ |
1093 |
|
public boolean replace(K key, V oldValue, V newValue) { |
1094 |
+ |
int hash = hash(key.hashCode()); |
1095 |
|
if (oldValue == null || newValue == null) |
1096 |
|
throw new NullPointerException(); |
1097 |
< |
int hash = hash(key); |
1098 |
< |
return segmentFor(hash).replace(key, hash, oldValue, newValue); |
1097 |
> |
Segment<K,V> s = segmentForHash(hash); |
1098 |
> |
return s != null && s.replace(key, hash, oldValue, newValue); |
1099 |
|
} |
1100 |
|
|
1101 |
|
/** |
1102 |
< |
* Replace entry for key only if currently mapped to some value. |
1103 |
< |
* Acts as |
1104 |
< |
* <pre> |
1105 |
< |
* if ((map.containsKey(key)) { |
1106 |
< |
* return map.put(key, value); |
947 |
< |
* } else return null; |
948 |
< |
* </pre> |
949 |
< |
* except that the action is performed atomically. |
950 |
< |
* @param key key with which the specified value is associated. |
951 |
< |
* @param value value to be associated with the specified key. |
952 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
953 |
< |
* if there was no mapping for key. |
954 |
< |
* @throws NullPointerException if the specified key or value is |
955 |
< |
* <tt>null</tt>. |
1102 |
> |
* {@inheritDoc} |
1103 |
> |
* |
1104 |
> |
* @return the previous value associated with the specified key, |
1105 |
> |
* or <tt>null</tt> if there was no mapping for the key |
1106 |
> |
* @throws NullPointerException if the specified key or value is null |
1107 |
|
*/ |
1108 |
|
public V replace(K key, V value) { |
1109 |
+ |
int hash = hash(key.hashCode()); |
1110 |
|
if (value == null) |
1111 |
|
throw new NullPointerException(); |
1112 |
< |
int hash = hash(key); |
1113 |
< |
return segmentFor(hash).replace(key, hash, value); |
1112 |
> |
Segment<K,V> s = segmentForHash(hash); |
1113 |
> |
return s == null ? null : s.replace(key, hash, value); |
1114 |
|
} |
1115 |
|
|
964 |
– |
|
1116 |
|
/** |
1117 |
< |
* Removes all mappings from this map. |
1117 |
> |
* Removes all of the mappings from this map. |
1118 |
|
*/ |
1119 |
|
public void clear() { |
1120 |
< |
for (int i = 0; i < segments.length; ++i) |
1121 |
< |
segments[i].clear(); |
1122 |
< |
} |
1123 |
< |
|
1124 |
< |
|
1125 |
< |
/** |
975 |
< |
* Returns a shallow copy of this |
976 |
< |
* <tt>ConcurrentHashMap</tt> instance: the keys and |
977 |
< |
* values themselves are not cloned. |
978 |
< |
* |
979 |
< |
* @return a shallow copy of this map. |
980 |
< |
*/ |
981 |
< |
public Object clone() { |
982 |
< |
// We cannot call super.clone, since it would share final |
983 |
< |
// segments array, and there's no way to reassign finals. |
984 |
< |
|
985 |
< |
float lf = segments[0].loadFactor; |
986 |
< |
int segs = segments.length; |
987 |
< |
int cap = (int)(size() / lf); |
988 |
< |
if (cap < segs) cap = segs; |
989 |
< |
ConcurrentHashMap<K,V> t = new ConcurrentHashMap<K,V>(cap, lf, segs); |
990 |
< |
t.putAll(this); |
991 |
< |
return t; |
1120 |
> |
final Segment<K,V>[] segments = this.segments; |
1121 |
> |
for (int j = 0; j < segments.length; ++j) { |
1122 |
> |
Segment<K,V> s = segmentAt(segments, j); |
1123 |
> |
if (s != null) |
1124 |
> |
s.clear(); |
1125 |
> |
} |
1126 |
|
} |
1127 |
|
|
1128 |
|
/** |
1129 |
< |
* Returns a set view of the keys contained in this map. The set is |
1130 |
< |
* backed by the map, so changes to the map are reflected in the set, and |
1131 |
< |
* vice-versa. The set supports element removal, which removes the |
1132 |
< |
* corresponding mapping from this map, via the <tt>Iterator.remove</tt>, |
1133 |
< |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and |
1134 |
< |
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
1129 |
> |
* Returns a {@link Set} view of the keys contained in this map. |
1130 |
> |
* The set is backed by the map, so changes to the map are |
1131 |
> |
* reflected in the set, and vice-versa. The set supports element |
1132 |
> |
* removal, which removes the corresponding mapping from this map, |
1133 |
> |
* via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
1134 |
> |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
1135 |
> |
* operations. It does not support the <tt>add</tt> or |
1136 |
|
* <tt>addAll</tt> operations. |
1137 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1138 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
1137 |
> |
* |
1138 |
> |
* <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator |
1139 |
> |
* that will never throw {@link ConcurrentModificationException}, |
1140 |
|
* and guarantees to traverse elements as they existed upon |
1141 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1142 |
|
* reflect any modifications subsequent to construction. |
1007 |
– |
* |
1008 |
– |
* @return a set view of the keys contained in this map. |
1143 |
|
*/ |
1144 |
|
public Set<K> keySet() { |
1145 |
|
Set<K> ks = keySet; |
1146 |
|
return (ks != null) ? ks : (keySet = new KeySet()); |
1147 |
|
} |
1148 |
|
|
1015 |
– |
|
1149 |
|
/** |
1150 |
< |
* Returns a collection view of the values contained in this map. The |
1151 |
< |
* collection is backed by the map, so changes to the map are reflected in |
1152 |
< |
* the collection, and vice-versa. The collection supports element |
1153 |
< |
* removal, which removes the corresponding mapping from this map, via the |
1154 |
< |
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
1155 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
1156 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
1157 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1158 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
1150 |
> |
* Returns a {@link Collection} view of the values contained in this map. |
1151 |
> |
* The collection is backed by the map, so changes to the map are |
1152 |
> |
* reflected in the collection, and vice-versa. The collection |
1153 |
> |
* supports element removal, which removes the corresponding |
1154 |
> |
* mapping from this map, via the <tt>Iterator.remove</tt>, |
1155 |
> |
* <tt>Collection.remove</tt>, <tt>removeAll</tt>, |
1156 |
> |
* <tt>retainAll</tt>, and <tt>clear</tt> operations. It does not |
1157 |
> |
* support the <tt>add</tt> or <tt>addAll</tt> operations. |
1158 |
> |
* |
1159 |
> |
* <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator |
1160 |
> |
* that will never throw {@link ConcurrentModificationException}, |
1161 |
|
* and guarantees to traverse elements as they existed upon |
1162 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1163 |
|
* reflect any modifications subsequent to construction. |
1029 |
– |
* |
1030 |
– |
* @return a collection view of the values contained in this map. |
1164 |
|
*/ |
1165 |
|
public Collection<V> values() { |
1166 |
|
Collection<V> vs = values; |
1167 |
|
return (vs != null) ? vs : (values = new Values()); |
1168 |
|
} |
1169 |
|
|
1037 |
– |
|
1170 |
|
/** |
1171 |
< |
* Returns a collection view of the mappings contained in this map. Each |
1172 |
< |
* element in the returned collection is a <tt>Map.Entry</tt>. The |
1173 |
< |
* collection is backed by the map, so changes to the map are reflected in |
1174 |
< |
* the collection, and vice-versa. The collection supports element |
1175 |
< |
* removal, which removes the corresponding mapping from the map, via the |
1176 |
< |
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
1177 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
1178 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
1179 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
1180 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
1171 |
> |
* Returns a {@link Set} view of the mappings contained in this map. |
1172 |
> |
* The set is backed by the map, so changes to the map are |
1173 |
> |
* reflected in the set, and vice-versa. The set supports element |
1174 |
> |
* removal, which removes the corresponding mapping from the map, |
1175 |
> |
* via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, |
1176 |
> |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> |
1177 |
> |
* operations. It does not support the <tt>add</tt> or |
1178 |
> |
* <tt>addAll</tt> operations. |
1179 |
> |
* |
1180 |
> |
* <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator |
1181 |
> |
* that will never throw {@link ConcurrentModificationException}, |
1182 |
|
* and guarantees to traverse elements as they existed upon |
1183 |
|
* construction of the iterator, and may (but is not guaranteed to) |
1184 |
|
* reflect any modifications subsequent to construction. |
1052 |
– |
* |
1053 |
– |
* @return a collection view of the mappings contained in this map. |
1185 |
|
*/ |
1186 |
|
public Set<Map.Entry<K,V>> entrySet() { |
1187 |
|
Set<Map.Entry<K,V>> es = entrySet; |
1188 |
< |
return (es != null) ? es : (entrySet = (Set<Map.Entry<K,V>>) (Set) new EntrySet()); |
1188 |
> |
return (es != null) ? es : (entrySet = new EntrySet()); |
1189 |
|
} |
1190 |
|
|
1060 |
– |
|
1191 |
|
/** |
1192 |
|
* Returns an enumeration of the keys in this table. |
1193 |
|
* |
1194 |
< |
* @return an enumeration of the keys in this table. |
1195 |
< |
* @see #keySet |
1194 |
> |
* @return an enumeration of the keys in this table |
1195 |
> |
* @see #keySet() |
1196 |
|
*/ |
1197 |
|
public Enumeration<K> keys() { |
1198 |
|
return new KeyIterator(); |
1201 |
|
/** |
1202 |
|
* Returns an enumeration of the values in this table. |
1203 |
|
* |
1204 |
< |
* @return an enumeration of the values in this table. |
1205 |
< |
* @see #values |
1204 |
> |
* @return an enumeration of the values in this table |
1205 |
> |
* @see #values() |
1206 |
|
*/ |
1207 |
|
public Enumeration<V> elements() { |
1208 |
|
return new ValueIterator(); |
1213 |
|
abstract class HashIterator { |
1214 |
|
int nextSegmentIndex; |
1215 |
|
int nextTableIndex; |
1216 |
< |
HashEntry[] currentTable; |
1216 |
> |
HashEntry<K,V>[] currentTable; |
1217 |
|
HashEntry<K, V> nextEntry; |
1218 |
|
HashEntry<K, V> lastReturned; |
1219 |
|
|
1223 |
|
advance(); |
1224 |
|
} |
1225 |
|
|
1226 |
< |
public boolean hasMoreElements() { return hasNext(); } |
1227 |
< |
|
1226 |
> |
/** |
1227 |
> |
* Sets nextEntry to first node of next non-empty table |
1228 |
> |
* (in backwards order, to simplify checks). |
1229 |
> |
*/ |
1230 |
|
final void advance() { |
1231 |
< |
if (nextEntry != null && (nextEntry = nextEntry.next) != null) |
1232 |
< |
return; |
1233 |
< |
|
1234 |
< |
while (nextTableIndex >= 0) { |
1235 |
< |
if ( (nextEntry = (HashEntry<K,V>)currentTable[nextTableIndex--]) != null) |
1236 |
< |
return; |
1237 |
< |
} |
1238 |
< |
|
1239 |
< |
while (nextSegmentIndex >= 0) { |
1240 |
< |
Segment<K,V> seg = (Segment<K,V>)segments[nextSegmentIndex--]; |
1109 |
< |
if (seg.count != 0) { |
1110 |
< |
currentTable = seg.table; |
1111 |
< |
for (int j = currentTable.length - 1; j >= 0; --j) { |
1112 |
< |
if ( (nextEntry = (HashEntry<K,V>)currentTable[j]) != null) { |
1113 |
< |
nextTableIndex = j - 1; |
1114 |
< |
return; |
1115 |
< |
} |
1116 |
< |
} |
1231 |
> |
for (;;) { |
1232 |
> |
if (nextTableIndex >= 0) { |
1233 |
> |
if ((nextEntry = entryAt(currentTable, |
1234 |
> |
nextTableIndex--)) != null) |
1235 |
> |
break; |
1236 |
> |
} |
1237 |
> |
else if (nextSegmentIndex >= 0) { |
1238 |
> |
Segment<K,V> seg = segmentAt(segments, nextSegmentIndex--); |
1239 |
> |
if (seg != null && (currentTable = seg.table) != null) |
1240 |
> |
nextTableIndex = currentTable.length - 1; |
1241 |
|
} |
1242 |
+ |
else |
1243 |
+ |
break; |
1244 |
|
} |
1245 |
|
} |
1246 |
|
|
1247 |
< |
public boolean hasNext() { return nextEntry != null; } |
1248 |
< |
|
1249 |
< |
HashEntry<K,V> nextEntry() { |
1124 |
< |
if (nextEntry == null) |
1247 |
> |
final HashEntry<K,V> nextEntry() { |
1248 |
> |
HashEntry<K,V> e = nextEntry; |
1249 |
> |
if (e == null) |
1250 |
|
throw new NoSuchElementException(); |
1251 |
< |
lastReturned = nextEntry; |
1252 |
< |
advance(); |
1253 |
< |
return lastReturned; |
1251 |
> |
lastReturned = e; // cannot assign until after null check |
1252 |
> |
if ((nextEntry = e.next) == null) |
1253 |
> |
advance(); |
1254 |
> |
return e; |
1255 |
|
} |
1256 |
|
|
1257 |
< |
public void remove() { |
1257 |
> |
public final boolean hasNext() { return nextEntry != null; } |
1258 |
> |
public final boolean hasMoreElements() { return nextEntry != null; } |
1259 |
> |
|
1260 |
> |
public final void remove() { |
1261 |
|
if (lastReturned == null) |
1262 |
|
throw new IllegalStateException(); |
1263 |
|
ConcurrentHashMap.this.remove(lastReturned.key); |
1265 |
|
} |
1266 |
|
} |
1267 |
|
|
1268 |
< |
final class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> { |
1269 |
< |
public K next() { return super.nextEntry().key; } |
1270 |
< |
public K nextElement() { return super.nextEntry().key; } |
1271 |
< |
} |
1272 |
< |
|
1273 |
< |
final class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> { |
1274 |
< |
public V next() { return super.nextEntry().value; } |
1275 |
< |
public V nextElement() { return super.nextEntry().value; } |
1268 |
> |
final class KeyIterator |
1269 |
> |
extends HashIterator |
1270 |
> |
implements Iterator<K>, Enumeration<K> |
1271 |
> |
{ |
1272 |
> |
public final K next() { return super.nextEntry().key; } |
1273 |
> |
public final K nextElement() { return super.nextEntry().key; } |
1274 |
> |
} |
1275 |
> |
|
1276 |
> |
final class ValueIterator |
1277 |
> |
extends HashIterator |
1278 |
> |
implements Iterator<V>, Enumeration<V> |
1279 |
> |
{ |
1280 |
> |
public final V next() { return super.nextEntry().value; } |
1281 |
> |
public final V nextElement() { return super.nextEntry().value; } |
1282 |
|
} |
1283 |
|
|
1149 |
– |
|
1150 |
– |
|
1284 |
|
/** |
1285 |
< |
* Entry iterator. Exported Entry objects must write-through |
1286 |
< |
* changes in setValue, even if the nodes have been cloned. So we |
1287 |
< |
* cannot return internal HashEntry objects. Instead, the iterator |
1288 |
< |
* itself acts as a forwarding pseudo-entry. |
1289 |
< |
*/ |
1290 |
< |
final class EntryIterator extends HashIterator implements Map.Entry<K,V>, Iterator<Entry<K,V>> { |
1291 |
< |
public Map.Entry<K,V> next() { |
1292 |
< |
nextEntry(); |
1160 |
< |
return this; |
1161 |
< |
} |
1162 |
< |
|
1163 |
< |
public K getKey() { |
1164 |
< |
if (lastReturned == null) |
1165 |
< |
throw new IllegalStateException("Entry was removed"); |
1166 |
< |
return lastReturned.key; |
1167 |
< |
} |
1168 |
< |
|
1169 |
< |
public V getValue() { |
1170 |
< |
if (lastReturned == null) |
1171 |
< |
throw new IllegalStateException("Entry was removed"); |
1172 |
< |
return ConcurrentHashMap.this.get(lastReturned.key); |
1285 |
> |
* Custom Entry class used by EntryIterator.next(), that relays |
1286 |
> |
* setValue changes to the underlying map. |
1287 |
> |
*/ |
1288 |
> |
final class WriteThroughEntry |
1289 |
> |
extends AbstractMap.SimpleEntry<K,V> |
1290 |
> |
{ |
1291 |
> |
WriteThroughEntry(K k, V v) { |
1292 |
> |
super(k,v); |
1293 |
|
} |
1294 |
|
|
1295 |
+ |
/** |
1296 |
+ |
* Sets our entry's value and writes through to the map. The |
1297 |
+ |
* value to return is somewhat arbitrary here. Since a |
1298 |
+ |
* WriteThroughEntry does not necessarily track asynchronous |
1299 |
+ |
* changes, the most recent "previous" value could be |
1300 |
+ |
* different from what we return (or could even have been |
1301 |
+ |
* removed in which case the put will re-establish). We do not |
1302 |
+ |
* and cannot guarantee more. |
1303 |
+ |
*/ |
1304 |
|
public V setValue(V value) { |
1305 |
< |
if (lastReturned == null) |
1306 |
< |
throw new IllegalStateException("Entry was removed"); |
1307 |
< |
return ConcurrentHashMap.this.put(lastReturned.key, value); |
1308 |
< |
} |
1180 |
< |
|
1181 |
< |
public boolean equals(Object o) { |
1182 |
< |
// If not acting as entry, just use default. |
1183 |
< |
if (lastReturned == null) |
1184 |
< |
return super.equals(o); |
1185 |
< |
if (!(o instanceof Map.Entry)) |
1186 |
< |
return false; |
1187 |
< |
Map.Entry e = (Map.Entry)o; |
1188 |
< |
return eq(getKey(), e.getKey()) && eq(getValue(), e.getValue()); |
1189 |
< |
} |
1190 |
< |
|
1191 |
< |
public int hashCode() { |
1192 |
< |
// If not acting as entry, just use default. |
1193 |
< |
if (lastReturned == null) |
1194 |
< |
return super.hashCode(); |
1195 |
< |
|
1196 |
< |
Object k = getKey(); |
1197 |
< |
Object v = getValue(); |
1198 |
< |
return ((k == null) ? 0 : k.hashCode()) ^ |
1199 |
< |
((v == null) ? 0 : v.hashCode()); |
1200 |
< |
} |
1201 |
< |
|
1202 |
< |
public String toString() { |
1203 |
< |
// If not acting as entry, just use default. |
1204 |
< |
if (lastReturned == null) |
1205 |
< |
return super.toString(); |
1206 |
< |
else |
1207 |
< |
return getKey() + "=" + getValue(); |
1305 |
> |
if (value == null) throw new NullPointerException(); |
1306 |
> |
V v = super.setValue(value); |
1307 |
> |
ConcurrentHashMap.this.put(getKey(), value); |
1308 |
> |
return v; |
1309 |
|
} |
1310 |
+ |
} |
1311 |
|
|
1312 |
< |
boolean eq(Object o1, Object o2) { |
1313 |
< |
return (o1 == null ? o2 == null : o1.equals(o2)); |
1312 |
> |
final class EntryIterator |
1313 |
> |
extends HashIterator |
1314 |
> |
implements Iterator<Entry<K,V>> |
1315 |
> |
{ |
1316 |
> |
public Map.Entry<K,V> next() { |
1317 |
> |
HashEntry<K,V> e = super.nextEntry(); |
1318 |
> |
return new WriteThroughEntry(e.key, e.value); |
1319 |
|
} |
1213 |
– |
|
1320 |
|
} |
1321 |
|
|
1322 |
|
final class KeySet extends AbstractSet<K> { |
1326 |
|
public int size() { |
1327 |
|
return ConcurrentHashMap.this.size(); |
1328 |
|
} |
1329 |
+ |
public boolean isEmpty() { |
1330 |
+ |
return ConcurrentHashMap.this.isEmpty(); |
1331 |
+ |
} |
1332 |
|
public boolean contains(Object o) { |
1333 |
|
return ConcurrentHashMap.this.containsKey(o); |
1334 |
|
} |
1347 |
|
public int size() { |
1348 |
|
return ConcurrentHashMap.this.size(); |
1349 |
|
} |
1350 |
+ |
public boolean isEmpty() { |
1351 |
+ |
return ConcurrentHashMap.this.isEmpty(); |
1352 |
+ |
} |
1353 |
|
public boolean contains(Object o) { |
1354 |
|
return ConcurrentHashMap.this.containsValue(o); |
1355 |
|
} |
1365 |
|
public boolean contains(Object o) { |
1366 |
|
if (!(o instanceof Map.Entry)) |
1367 |
|
return false; |
1368 |
< |
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
1368 |
> |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
1369 |
|
V v = ConcurrentHashMap.this.get(e.getKey()); |
1370 |
|
return v != null && v.equals(e.getValue()); |
1371 |
|
} |
1372 |
|
public boolean remove(Object o) { |
1373 |
|
if (!(o instanceof Map.Entry)) |
1374 |
|
return false; |
1375 |
< |
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
1375 |
> |
Map.Entry<?,?> e = (Map.Entry<?,?>)o; |
1376 |
|
return ConcurrentHashMap.this.remove(e.getKey(), e.getValue()); |
1377 |
|
} |
1378 |
|
public int size() { |
1379 |
|
return ConcurrentHashMap.this.size(); |
1380 |
|
} |
1381 |
+ |
public boolean isEmpty() { |
1382 |
+ |
return ConcurrentHashMap.this.isEmpty(); |
1383 |
+ |
} |
1384 |
|
public void clear() { |
1385 |
|
ConcurrentHashMap.this.clear(); |
1386 |
|
} |
1272 |
– |
public Object[] toArray() { |
1273 |
– |
// Since we don't ordinarily have distinct Entry objects, we |
1274 |
– |
// must pack elements using exportable SimpleEntry |
1275 |
– |
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1276 |
– |
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1277 |
– |
c.add(new SimpleEntry<K,V>(i.next())); |
1278 |
– |
return c.toArray(); |
1279 |
– |
} |
1280 |
– |
public <T> T[] toArray(T[] a) { |
1281 |
– |
Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>(size()); |
1282 |
– |
for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) |
1283 |
– |
c.add(new SimpleEntry<K,V>(i.next())); |
1284 |
– |
return c.toArray(a); |
1285 |
– |
} |
1286 |
– |
|
1287 |
– |
} |
1288 |
– |
|
1289 |
– |
/** |
1290 |
– |
* This duplicates java.util.AbstractMap.SimpleEntry until this class |
1291 |
– |
* is made accessible. |
1292 |
– |
*/ |
1293 |
– |
static final class SimpleEntry<K,V> implements Entry<K,V> { |
1294 |
– |
K key; |
1295 |
– |
V value; |
1296 |
– |
|
1297 |
– |
public SimpleEntry(K key, V value) { |
1298 |
– |
this.key = key; |
1299 |
– |
this.value = value; |
1300 |
– |
} |
1301 |
– |
|
1302 |
– |
public SimpleEntry(Entry<K,V> e) { |
1303 |
– |
this.key = e.getKey(); |
1304 |
– |
this.value = e.getValue(); |
1305 |
– |
} |
1306 |
– |
|
1307 |
– |
public K getKey() { |
1308 |
– |
return key; |
1309 |
– |
} |
1310 |
– |
|
1311 |
– |
public V getValue() { |
1312 |
– |
return value; |
1313 |
– |
} |
1314 |
– |
|
1315 |
– |
public V setValue(V value) { |
1316 |
– |
V oldValue = this.value; |
1317 |
– |
this.value = value; |
1318 |
– |
return oldValue; |
1319 |
– |
} |
1320 |
– |
|
1321 |
– |
public boolean equals(Object o) { |
1322 |
– |
if (!(o instanceof Map.Entry)) |
1323 |
– |
return false; |
1324 |
– |
Map.Entry e = (Map.Entry)o; |
1325 |
– |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
1326 |
– |
} |
1327 |
– |
|
1328 |
– |
public int hashCode() { |
1329 |
– |
return ((key == null) ? 0 : key.hashCode()) ^ |
1330 |
– |
((value == null) ? 0 : value.hashCode()); |
1331 |
– |
} |
1332 |
– |
|
1333 |
– |
public String toString() { |
1334 |
– |
return key + "=" + value; |
1335 |
– |
} |
1336 |
– |
|
1337 |
– |
static boolean eq(Object o1, Object o2) { |
1338 |
– |
return (o1 == null ? o2 == null : o1.equals(o2)); |
1339 |
– |
} |
1387 |
|
} |
1388 |
|
|
1389 |
|
/* ---------------- Serialization Support -------------- */ |
1390 |
|
|
1391 |
|
/** |
1392 |
< |
* Save the state of the <tt>ConcurrentHashMap</tt> |
1393 |
< |
* instance to a stream (i.e., |
1347 |
< |
* serialize it). |
1392 |
> |
* Saves the state of the <tt>ConcurrentHashMap</tt> instance to a |
1393 |
> |
* stream (i.e., serializes it). |
1394 |
|
* @param s the stream |
1395 |
|
* @serialData |
1396 |
|
* the key (Object) and value (Object) |
1397 |
|
* for each key-value mapping, followed by a null pair. |
1398 |
|
* The key-value mappings are emitted in no particular order. |
1399 |
|
*/ |
1400 |
< |
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
1400 |
> |
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
1401 |
> |
// force all segments for serialization compatibility |
1402 |
> |
for (int k = 0; k < segments.length; ++k) |
1403 |
> |
ensureSegment(k); |
1404 |
|
s.defaultWriteObject(); |
1405 |
|
|
1406 |
+ |
final Segment<K,V>[] segments = this.segments; |
1407 |
|
for (int k = 0; k < segments.length; ++k) { |
1408 |
< |
Segment<K,V> seg = (Segment<K,V>)segments[k]; |
1408 |
> |
Segment<K,V> seg = segmentAt(segments, k); |
1409 |
|
seg.lock(); |
1410 |
|
try { |
1411 |
< |
HashEntry[] tab = seg.table; |
1411 |
> |
HashEntry<K,V>[] tab = seg.table; |
1412 |
|
for (int i = 0; i < tab.length; ++i) { |
1413 |
< |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; e != null; e = e.next) { |
1413 |
> |
HashEntry<K,V> e; |
1414 |
> |
for (e = entryAt(tab, i); e != null; e = e.next) { |
1415 |
|
s.writeObject(e.key); |
1416 |
|
s.writeObject(e.value); |
1417 |
|
} |
1425 |
|
} |
1426 |
|
|
1427 |
|
/** |
1428 |
< |
* Reconstitute the <tt>ConcurrentHashMap</tt> |
1429 |
< |
* instance from a stream (i.e., |
1379 |
< |
* deserialize it). |
1428 |
> |
* Reconstitutes the <tt>ConcurrentHashMap</tt> instance from a |
1429 |
> |
* stream (i.e., deserializes it). |
1430 |
|
* @param s the stream |
1431 |
|
*/ |
1432 |
+ |
@SuppressWarnings("unchecked") |
1433 |
|
private void readObject(java.io.ObjectInputStream s) |
1434 |
< |
throws IOException, ClassNotFoundException { |
1434 |
> |
throws IOException, ClassNotFoundException { |
1435 |
|
s.defaultReadObject(); |
1436 |
|
|
1437 |
< |
// Initialize each segment to be minimally sized, and let grow. |
1438 |
< |
for (int i = 0; i < segments.length; ++i) { |
1439 |
< |
segments[i].setTable(new HashEntry[1]); |
1437 |
> |
// Re-initialize segments to be minimally sized, and let grow. |
1438 |
> |
int cap = MIN_SEGMENT_TABLE_CAPACITY; |
1439 |
> |
final Segment<K,V>[] segments = this.segments; |
1440 |
> |
for (int k = 0; k < segments.length; ++k) { |
1441 |
> |
Segment<K,V> seg = segments[k]; |
1442 |
> |
if (seg != null) { |
1443 |
> |
seg.threshold = (int)(cap * seg.loadFactor); |
1444 |
> |
seg.table = (HashEntry<K,V>[]) new HashEntry<?,?>[cap]; |
1445 |
> |
} |
1446 |
|
} |
1447 |
|
|
1448 |
|
// Read the keys and values, and put the mappings in the table |
1454 |
|
put(key, value); |
1455 |
|
} |
1456 |
|
} |
1400 |
– |
} |
1457 |
|
|
1458 |
+ |
// Unsafe mechanics |
1459 |
+ |
private static final sun.misc.Unsafe UNSAFE; |
1460 |
+ |
private static final long SBASE; |
1461 |
+ |
private static final int SSHIFT; |
1462 |
+ |
private static final long TBASE; |
1463 |
+ |
private static final int TSHIFT; |
1464 |
+ |
|
1465 |
+ |
static { |
1466 |
+ |
int ss, ts; |
1467 |
+ |
try { |
1468 |
+ |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
1469 |
+ |
Class<?> tc = HashEntry[].class; |
1470 |
+ |
Class<?> sc = Segment[].class; |
1471 |
+ |
TBASE = UNSAFE.arrayBaseOffset(tc); |
1472 |
+ |
SBASE = UNSAFE.arrayBaseOffset(sc); |
1473 |
+ |
ts = UNSAFE.arrayIndexScale(tc); |
1474 |
+ |
ss = UNSAFE.arrayIndexScale(sc); |
1475 |
+ |
} catch (Exception e) { |
1476 |
+ |
throw new Error(e); |
1477 |
+ |
} |
1478 |
+ |
if ((ss & (ss-1)) != 0 || (ts & (ts-1)) != 0) |
1479 |
+ |
throw new Error("data type scale not a power of two"); |
1480 |
+ |
SSHIFT = 31 - Integer.numberOfLeadingZeros(ss); |
1481 |
+ |
TSHIFT = 31 - Integer.numberOfLeadingZeros(ts); |
1482 |
+ |
} |
1483 |
+ |
|
1484 |
+ |
} |