...
Wiki Markup |
---|
This technique is also called client-side locking \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\] because the class holds a lock on an object that might be accessible to other classes. Client-side locking is not always an appropriate strategy; see rule [LCK11-J. Avoid client-side locking when using classes that do not commit to their locking strategy] for more information. |
...
Wiki Markup |
---|
This noncompliant code example defines the {{KeyedCounter}} class that is not thread-safe. Although the {{HashMap}} is wrapped in a {{synchronizedMap()}}, the overall increment operation is not be atomic \[[Lee 2009|AA. Bibliography#LeeReferences#Lee 09]\]. |
Code Block | ||
---|---|---|
| ||
final class KeyedCounter { private final Map<String, Integer> map = Collections.synchronizedMap(new HashMap<String, Integer>()); public void increment(String key) { Integer old = map.get(key); int oldValue = (old == null) ? 0 : old.intValue(); if (oldValue == Integer.MAX_VALUE) { throw new ArithmeticException("Out of range"); } map.put( key, oldValue + 1); } public Integer getCount(String key) { return map.get(key); } } |
...
Wiki Markup |
---|
The {{ConcurrentHashMap}} class used in this compliant solution provides several utility methods for performing atomic operations and is often a good choice for algorithms that must scale \[[Lee 2009|AA. Bibliography#LeeReferences#Lee 09]\]. |
Code Block | ||
---|---|---|
| ||
final class KeyedCounter { private final ConcurrentMap<String, AtomicInteger> map = new ConcurrentHashMap<String, AtomicInteger>(); public void increment(String key) { AtomicInteger value = new AtomicInteger(); AtomicInteger old = map.putIfAbsent(key, value); if (old != null) { value = old; } if (value.get() == Integer.MAX_VALUE) { throw new ArithmeticException("Out of range"); } value.incrementAndGet(); // Increment the value atomically } public Integer getCount(String key) { AtomicInteger value = map.get(key); return (value == null) ? null : value.get(); } // Other accessors ... } |
Wiki Markup |
---|
According to Section 5.2.1., "ConcurrentHashMap" of the work of Goetz and colleagues \[[Goetz 2006|AA. Bibliography#GoetzReferences#Goetz 06]\]: |
ConcurrentHashMap
, along with the other concurrent collections, further improve on the synchronized collection classes by providing iterators that do not throwConcurrentModificationException
, as a result eliminating the need to lock the collection during iteration. The iterators returned byConcurrentHashMap
are weakly consistent instead of fail-fast. A weakly consistent iterator can tolerate concurrent modification, traverses elements as they existed when the iterator was constructed, and may (but is not guaranteed to) reflect modifications to the collection after the construction of the iterator.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d512954da07e7b84-7d87cef1-474243c8-9e498303-3d6db4965437f3904f15dcda"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API References#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4094af7b3f7a838a-bc737957-4a304ad0-82249ad0-08ece5355389009267119601"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#Goetz References#Goetz 06]] | Section 4.4.1, Client-side Locking | ]]></ac:plain-text-body></ac:structured-macro> |
| Section 5.2.1, ConcurrentHashMap | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b1173f017cf741c7-7c0426b3-47b64b22-a48ca4fb-3de75bad7f0a3820e9ba2115"><ac:plain-text-body><![CDATA[ | [[JavaThreads 2004 | AA. Bibliography#JavaThreads References#JavaThreads 04]] | Section 8.2, Synchronization and Collection Classes | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="902a21c2a4771d7b-4805b687-43094c5f-b3b19fe2-16587d7d4d44bca3a4662686"><ac:plain-text-body><![CDATA[ | [[Lee 2009 | AA. Bibliography#Lee References#Lee 09]] | Map & Compound Operation | ]]></ac:plain-text-body></ac:structured-macro> |
...