...
Code Block | ||
---|---|---|
| ||
class CountBoxes implements Runnable {
static int counter;
// ...
static final Object lock = new Object();
public void run() {
synchronized(lock) {
counter++;
// ...
}
// ...
}
|
There is no requirement of declaring the counter
variable as volatile
when synchronization is used.
Compliant Solution (2) (intrinsic lock of class object)
This compliant solution uses the intrinsic lock of the class to synchronize the increment operation.
...