...
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 | ||
---|---|---|
| ||
// 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="f91f646d9892e859-4d8f01e3-4be04cbb-b72e80a5-4117328363cdc70352e279ba"><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="dfcc88ef60c0d4bf-d84221d6-4cea421a-9c9497ed-28bc787895015b7069e5e3dd"><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="5599e5c2bc3c750a-0799482c-46934beb-9e499987-1eb759ec37c8202bf21cd4f7"><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="45975c13ae8af1ab-e75f1fb9-47db407c-9d7bb335-9ed9a0d8fa62f26dfe489d56"><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="d1eda8513dd43c37-eb04148d-420e4097-8830a305-f3774574d26106076db7badf"><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> |
...