Consider that 10 threads increase int i (initialized to 0) and display the console value. You can get something like this:
1 2 2 3 3 5 6 6 8 10
AtomicInteger, for example, ensures that each thread can increase or decrease the value atomically , ensuring that the write operation is performed synchronously, and for 10 threads the output will always be:
1 2 3 4 5 6 7 8 9 10
SiN
source share