...
This compliant solution uses a static private final lock to protect the counter field and, consequently, does not depend on any external synchronization. This solution also complies with CON07 LCK00-J. Use private final lock objects to synchronize classes that may interact with untrusted code.
| Code Block | ||
|---|---|---|
| ||
/** This class is thread-safe */
public final class CountHits {
private static int counter;
private static final Object lock = new Object();
public void incrementCounter() {
synchronized (lock) {
counter++;
}
}
}
|
...