What is the exact use of classes in java.util.concurrent.atomic package in Java?

I am a relatively new java. I am trying to understand what the use of classes in a package is:

java.util.concurrent.atomic

I tried to understand javaDoc for this package to understand it. But I could not make any sense out of it when I should use these classes. Can anyone give examples and descriptions in simple words? THX

+7
source share
1 answer

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 
+11
source

All Articles