Versions Compared

Key

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

...

The Boolean type is unsuitable for locking purposes because it allows only two values: true and false. Boolean literals containing the same value share unique instances of the Boolean class in the Java Virtual Machine (JVM). In this example, initialized refers to the instance corresponding to the value false Boolean.FALSE. If any other code were to inadvertently synchronize on a Boolean literal with the this value false, the lock instance would be reused and the system could become unresponsive or could deadlock.

...

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

...

When explicitly constructed, an Integer object has a unique reference and its own intrinsic lock that is distinct not only from other Integer objects, but also from boxed integers that have the same value. While this is an acceptable solution, it can cause maintenance problems because developers can incorrectly assume that boxed integers are also appropriate lock objects. A more appropriate solution is to synchronize on a private final lock object as described in the final compliant solution belowfor this rule.

Noncompliant Code Example (Interned String Object)

...

Wiki Markup
According to the Java API class  {{java.lang.String}} documentation \[[API 2006|AA. Bibliography#API 06]\] {{java.lang.String}} documentation:

When the intern() method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

...

Additionally, hostile code from any other package can exploit this vulnerability, if the class is accessible. (For more information, see See rule LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code for more information.)

Noncompliant Code Example (String Literal)

...

This compliant solution locks on a non-interned noninterned String instance.

Code Block
bgColor#ccccff
private final String lock = new String("LOCK");

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

A String instance differs from a String literal. The instance has a unique reference and its own intrinsic lock that is distinct from other String object instances or literals. Nevertheless, a better approach is to synchronize on a private final lock object, as shown in the following compliant solution.

...

This compliant solution synchronizes on a private final lock object. This is one of the few cases where in which a java.lang.Object instance is useful.

...

A significant number of concurrency vulnerabilities arise from locking on the wrong kind of object. It is important to consider the properties of the lock object rather than indiscreetly simply scavenging for objects on which to synchronize.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

LCK01-J

medium

probable

medium

P8

L2

Automated Detection

The following table summarizes the examples flagged as violations by FindBugs:

...

Noncompliant Code Example

...

Flagged

...

Checker

...

Message

...

Boolean lock object

...

Yes

...

DL_SYNCHRONIZATION_ON_BOOLEAN

...

Synchronization on Boolean could deadlock

...

Boxed primitive

...

Yes

...

DL_SYNCHRONIZATION_ON_BOXED_PRIMITIVE

...

Synchronization on Integer could deadlock

...

interned String object

...

No

...

n/a

...

n/a

...

String literal

...

Yes

...

DL_SYNCHRONIZATION_ON_SHARED_CONSTANT

...

Some static analysis tools can detect violations of this rule.

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="d7382c8ff7399474-18906820-4fb3463f-8f2581bd-ff4bd754fb0941aad87aae83"><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="7f763b68e812e5e8-a8686e42-4def40d3-9fb792d9-8201fe9b0945ec80425763bd"><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="984db5faede23ddf-b488900a-488d4fe4-80e59152-e74f306ab64dabd26751278f"><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="da18219500506df2-9ed5b54f-4f5842d9-8738af3c-0580a7c2fe96bac7e50a4a55"><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="eb68c7e77518aa34-3434d8f2-4275448a-9291a8a2-934165777e70d562c202de5f"><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>

...