Versions Compared

Key

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

...

Boxed types may use the same instance for a range of integer values; consequently, they suffer from the same reuse problem as Boolean constants. The wrapper object are reused when the value can be represented as a byte; JVM implementations are also permitted to reuse wrapper objects for larger ranges of values. Note that While use of the intrinsic lock associated with the boxed Integer wrapper object is insecure; instances of the Integer object constructed using the new operator (new Integer(value)) are unique and not reused. In general, locks on any data type that contains a boxed value are insecure.

Compliant Solution (Integer)

This compliant solution recommends locking locks on a nonboxed Integer, using a variant of the private lock object idiom. The doSomething() method synchronizes using the intrinsic lock of the Integer instance, Lock.

...

Code Block
bgColor#FFcccc
// This bug was found in jetty-6.1.3 BoundedThreadPool
private final String lock = "LOCK";

// ...public void doSomething() {
  synchronized (lock) {
    // ...
  }
// ...}

String literals are constant and are automatically interned. Consequently, this example suffers from the same pitfalls as the preceding noncompliant code example.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6a061ff4468fdd76-44b0a401-47ae48aa-91598605-8f67ec22bd9077b54383e797"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

Class String, Collections

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1af21d7c17d4c981-05ad34d4-447d4faa-96379dbe-7d9c9bbf9c22436537540f5e"><ac:plain-text-body><![CDATA[

[[Findbugs 2008

AA. Bibliography#Findbugs 08]]

 

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a8a82a3588e39f63-4825c85e-47e24a11-a09d9845-1164cbbd39237f17684968b0"><ac:plain-text-body><![CDATA[

[[Miller 2009

AA. Bibliography#Miller 09]]

Locking

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1f06d538a81f4f43-6c378cb6-4ed14a40-83ff8540-e7615d6eb8f792ff7bad312d"><ac:plain-text-body><![CDATA[

[[Pugh 2008

AA. Bibliography#Pugh 08]]

Synchronization

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="360c8099d65993a4-7ca7609d-40ab4de3-a4ca8fb7-c2a433f68623bf0a0aa43193"><ac:plain-text-body><![CDATA[

[[Tutorials 2008

AA. Bibliography#Tutorials 08]]

[Wrapper Implementations

http://java.sun.com/docs/books/tutorial/collections/implementations/wrapper.html]

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

...