...
Code Block | ||
---|---|---|
| ||
public final class CountBoxes implements Runnable {
private static volatile int counter;
// ...
private final Object lock = new Object();
@Override public void run() {
synchronized (lock) {
counter++;
// ...
}
}
public static void main(String[] args) {
for(int i = 0; i < 2; i++) {
new Thread(new CountBoxes()).start();
}
}
}
|
...
Code Block | ||
---|---|---|
| ||
public class CountBoxes implements Runnable { private static int counter; // ... private static final Object lock = new Object(); public void run() { synchronized (lock) { counter++; // ... } } // ... } |
It is unnecessary to declare the counter
variable volatile when using synchronization.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5e81ab38fc85caf2-0493e697-407c4f38-98a491da-0052e9e8943a4057b8bf45c5"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | ]]></ac:plain-text-body></ac:structured-macro> |
...