Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: wordsmithing

...

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
bgColor#ccccff
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="797fffd75acf67a6-b54bdf60-48c84d76-9d45b4c7-b51cc6ddae6fba0c1b73c935"><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="06109b19eedd9b1b-1aa17317-44c14d54-82b98f47-327b7ac379ab54668c07542e"><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="8861b69af144b79c-6fcf3230-4a5543be-b3168e9c-719a9ef49f33c8f6af93c5ed"><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="d9b121bc545a0da4-081ef743-47064113-a492ae3c-d3e68f12dd6633aebce9796c"><ac:plain-text-body><![CDATA[

[[Lee 2009

AA. Bibliography#Lee 09]]

"Map & Compound Operation"

]]></ac:plain-text-body></ac:structured-macro>

...