...
Consider, for example, a scenario where the standard thread-safe API lacks a single method to both find a particular person's record in a Hashtable and also update the corresponding that person's payroll information. In such cases, the two method invocations must be performed atomically.
...
| Code Block | ||
|---|---|---|
| ||
final class Adder {
// ...
private final AtomicReference<BigInteger> first;
private final AtomicReference<BigInteger> second;
public Adder(BigInteger f, BigInteger s) {
first = new AtomicReference<BigInteger>(f);
second = new AtomicReference<BigInteger>(s);
}
public synchronized void update(BigInteger f, BigInteger s){
first.set(f);
second.set(s);
}
public synchronized BigInteger add() {
return first.get().add(second.get());
}
}
|
Noncompliant Code Example (synchronizedList())
This noncompliant code example uses a java.util.ArrayList<E> collection, which is not thread-safe. However, the example uses Collections.synchronizedList as a synchronization wrapper for the ArrayList. It subsequently uses an array, rather than an iterator, to iterate over the ArrayList to avoid a ConcurrentModificationException.
...
This code does not violate rule LCK04-J. Do not synchronize on a collection view if the backing collection is accessible because, while it does synchronize on a collection view (the synchronizedList), the backing collection is inaccessible and consequently cannot be modified by any code.
Noncompliant Code Example (synchronizedMap())
| 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 fails to be atomic \[[Lee 2009|AA. Bibliography#Lee 09]\]. |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e47a77b6d9e9d9f5-dc3f7e89-45494c73-a75fad30-c1f7220070edac79f92e4dbf"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="28ce9d70f54cd73d-50142077-43744e01-9c0f9e09-c9335c2098740464679e9ce5"><ac:plain-text-body><![CDATA[ | [[Goetz 2006 | AA. Bibliography#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="19e7a03a55d44226-8ac894e1-4fb44a65-9cc7ba6b-8572575fca77a74b31f37ef3"><ac:plain-text-body><![CDATA[ | [[JavaThreads 2004 | AA. Bibliography#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="017667ec747fd534-a4dfb453-4f3d4361-b8bfa4a6-41a9f671fc824a99ce9709b0"><ac:plain-text-body><![CDATA[ | [[Lee 2009 | AA. Bibliography#Lee 09]] | "Map & Compound Operation" | ]]></ac:plain-text-body></ac:structured-macro> |
...