Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Individually, the add() and toArray() collection methods are atomic. However, when called in succession (for example, as shown in the addAndPrintIPAddresses() method), there is no guarantee that the combined operation is atomic. The addAndPrintIPAddresses() method contains a race condition that allows one thread to add to the list and a second thread to race in and modify the list before the first thread completes. Consequently, the addressCopy array may contain more IP addresses than expected.

...

Note that this compliant solution does not actually use the synchronization offered by Collections.synchronizedList(). If no other code in this solution used it, it could be removed, with ips being a direct ArrayListeliminated.

Noncompliant Code Example (synchronizedMap())

...

This compliant solution avoids using Collections.synchronizedMap() because locking on the unsynchronized map provides sufficient thread-safety for this application. Rule LCK04-J. Do not synchronize on a collection view if the backing collection is accessible provides more information about synchronizing on synchronizedMap() objects.

Compliant Solution (ConcurrentHashMap)

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cbb28b9561420a35-a4e3d1e0-456e447b-bb9fb9dd-99806019439590a10cc44c49"><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="8b9100ba10d7b7c4-a215f8be-4e364748-88899832-d1057b33ad67021a49f880ee"><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="7cc0917ad957f301-1f013379-43684f22-8f9296ea-f97a3c72ed4ab4ae5aaf1981"><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="167afa1b8493dd02-9a7837b3-43c5420c-a0b786e7-00f0d8ff57b1a1bedaa149db"><ac:plain-text-body><![CDATA[

[[Lee 2009

AA. Bibliography#Lee 09]]

Map & Compound Operation

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

...