...
Programs
...
are
...
forbidden
...
to
...
lock
...
on
...
an
...
object
...
of
...
a
...
class
...
that
...
implements
...
either
...
or
...
both
...
of
...
the
...
Lock
...
and
...
Condition
...
interfaces
...
of
...
the
...
java.util.concurrent.locks
...
package.
...
Using
...
the
...
intrinsic
...
locks
...
of
...
these
...
classes
...
is
...
a
...
questionable
...
practice
...
even
...
in
...
cases
...
where
...
the
...
code
...
may
...
appear
...
to
...
function
...
correctly.
...
This
...
problem
...
generally
...
arises
...
when
...
code
...
is
...
refactored
...
from
...
intrinsic
...
locking
...
to
...
the
...
java.util.concurrent
...
dynamic-locking
...
utilities.
...
Noncompliant
...
Code
...
Example
...
(
...
ReentrantLock
...
Lock
...
Object)
...
The
...
doSomething()
...
method
...
in
...
this
...
noncompliant
...
code
...
example
...
synchronizes
...
on
...
the
...
intrinsic
...
lock
...
of
...
an
...
instance
...
of
...
ReentrantLock
...
rather
...
than
...
on
...
the
...
reentrant
...
mutual
...
exclusion
...
Lock
...
encapsulated
...
by
...
ReentrantLock
...
.
| Code Block | ||||
|---|---|---|---|---|
| =
| |||
} private final Lock lock = new ReentrantLock(); public void doSomething() { synchronized(lock) { // ... } } {code} h2. Compliant Solution ({{ |
Compliant Solution (lock()
...
and
...
unlock()
...
)
...
This
...
compliant
...
solution
...
uses
...
the
...
lock()
...
and
...
unlock()
...
methods
...
provided
...
by
...
the
...
Lock
...
interface.
| Code Block | ||||
|---|---|---|---|---|
| =
| |||
} private final Lock lock = new ReentrantLock(); public void doSomething() { lock.lock(); try { // ... } finally { lock.unlock(); } } {code} |
In
...
the
...
absence
...
of
...
a
...
requirement
...
for
...
the
...
advanced
...
functionality
...
of
...
the
...
java.util.concurrent
...
package's
...
dynamic-locking
...
utilities,
...
it
...
is
...
better
...
to
...
use
...
the
...
Executor
...
framework
...
or
...
other
...
concurrency
...
primitives
...
such
...
as
...
synchronization
...
and
...
atomic
...
classes.
...
Risk
...
Assessment
...
Synchronizing
...
on
...
the
...
intrinsic
...
lock
...
of
...
high-level
...
concurrency
...
utilities
...
can
...
cause
...
nondeterministic
...
behavior
...
because
...
the
...
class
...
can
...
end
...
up
...
with
...
two
...
different
...
locking
...
policies.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
LCK03-J | medium | probable | medium | P8 | L2 |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0f98267f-c426-4d54-8cc4-a3b831d99ea8"><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="add2acd3-a2ae-4cdc-b418-235945c3f29b"><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="332c7f10-347c-45a0-b8ae-701aa572ad06"><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="32d0a3ad-09ee-43c3-b69e-6c6ec95b1f23"><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="0f6920e7-b75e-4d26-a4e2-e41e73a9fe4d"><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> |
...