...
Noncompliant Code Example (volatile
)
In this noncompliant code example, the volatile
field itemsInInventory
can be accessed by multiple threads. However, when a thread is updating the value of itemsInInventory
, it is possible for other threads to read the original value (that is, the value before the update). This is because the post decrement operator is non-atomic.
Code Block |
---|
|
private volatile int itemsInInventory = 100;
public int removeItem() {
if(itemsInInventory > 0) {
return itemsInInventory--; // Returns new count of items in inventory
} else
{
return -1; // returnError 0;
code }
}
|
Compliant Solution (1) (java.util.concurrent.atomic classes)
...
Code Block |
---|
|
public class Sync {
private final AtomicInteger itemsInInventory = new AtomicInteger(100);
private int removeItem() {
for (;;) {
int old = itemsInInventory.get();
if (old > 0) {
int next = old - 1;
if (itemsInInventory.compareAndSet(old, next)) {
return next; //returns new count of items in inventory
}
} else {
return 0;-1; // Error code
}
}
}
}
|
Compliant Solution (2) (method synchronization)
...
Code Block |
---|
|
private int itemsInInventory = 100;
public synchronized int removeItem() {
if(itemsInInventory > 0) {
return itemsInInventory--; // Returns new count of items in inventory
} else {
return -1; return 0;
}// Error Code
}
|
Wiki Markup |
---|
Synchronization is more expensive than using the optimized {{java.util.concurrent}} utilities and should only be used when the utilities do not contain the required method to carry out the atomic operation. When using explicit synchronization, the programmer must also ensure that two or more threads are not mutually accessible from a different set of two or more threads such that each thread holds a lock while trying to obtain another lock that is held by the other thread \[[Lea 00|AA. Java References#Lea 00]\]. Failure to follow this advice results in deadlocks ([CON12-J. Avoid deadlock by requesting and releasing locks in the same order]). |
...
Code Block |
---|
|
private volatile int itemsInInventory = 100;
public int removeItem() {
synchronized(this) {
if(itemsInInventory > 0) {
return itemsInInventory--; // Returns new count of items in inventory
} else {
return 0-1;
// Error }code
}
}
|
Block synchronization is more preferable than over method synchronization because it reduces the period duration for which the lock is held and also protects against denial of service attacks. The variable itemsInInventory
still needs to be declared volatile
because the check to determine whether it is greater than 0 relies on the latest value of the variable. An alternative to avoid the need to declare the variable volatile
is to use block synchronization across the whole if-else
block. However, this alternative is more costly.
...
Code Block |
---|
|
public class Sync {
private int itemsInInventory = 100;
private final Lock lock = new ReentrantLock();
public int removeItem() {
Boolean myLock = false;
try {
myLock = lock.tryLock();
if(itemsInInventory > 0) {
return itemsInInventory--;
}
} finally {
if (myLock) {
lock.unlock();
}
}
return 0;-1; // Error code
}
}
|
Code that uses this lock behaves similar to synchronized code that uses the traditional monitor lock. In addition, it provides several other capabilities, for instance, the tryLock()
method does not block waiting if another thread is already holding the lock. The class java.util.concurrent.locks.ReentrantReadWriteLock
can be used when a thread requires a lock to write information while other threads require the lock to simultaneously read the information.
Noncompliant Code Example (AtomicReference
)
This noncompliant code example uses two AtomicReference
objects to hold two BigInteger
object references.
...