...
Wiki Markup |
---|
Read-write locks allow shared state to be accessed by multiple readers or a single writer but never both. According to Goetz \[[Goetz 06|AA. Java References#Goetz 06]\]: |
In practice, read-write locks can improve performance for frequently accessed read-mostly data structures on multiprocessor systems; under other conditions they perform slightly worse than exclusive locks due to their greater complexity.
...
The flag
variable is updated using the compareAndSet()
method of the AtomicBoolean
class. All updates are visible to other threads.
Noncompliant Code Example (Addition of
...
Primitives)
In this noncompliant code example, multiple threads can invoke the setValues()
method to set the a
and b
fields. Because this class does not test for integer overflow, a user of the Adder
class must ensure that the arguments to the setValues()
method can be added without overflow. (For more information, see INT00-J. Perform explicit range checking to ensure integer operations do not overflow.)
...
Note that declaring the variables as volatile does not resolve the issue because these compound operations involve reads and writes of multiple variables.
Noncompliant Code Example (Addition of
...
Atomic Integers)
In this noncompliant code example, a
and b
are replaced with atomic integers.
...