Data races
Instead of starting by explaining atomic classes, let’s start with explaining a problem called a data race with an example. We already have seen how to fix this problem with the use of atomic classes, synchronized keyword, and locks. Can you spot the problem in the following code snippet?
public class Main {
private static int counter = 0; public static void main(String[] args) throws
InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 10000; i++) {
counter++;
}
});
Thread...