I am relatively new java. I am trying understand what are the usage of classes in the package:
java.util.concurrent.atomic
I tried to understand the javaDoc for this package to get a grasp of it. But could'nt really make any sense out of it to when I should use these classes. Can someone give examples and more descriptions in simple words? thx
Consider 10 threads are incrementing int i (initialized at 0) and outputting the value the console. You can get something like this:
1
2
2
3
3
5
6
6
8
10
AtomicInteger, for example, ensures that each thread can increment or decrement the value atomically, ensuring that the write operation happens in a synchronized manner, and for 10 threads, the output would always be:
1
2
3
4
5
6
7
8
9
10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With