Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 460
Hossein Avatar asked Sep 10 '12 21:09

Hossein


1 Answers

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
like image 114
SiN Avatar answered Sep 28 '22 07:09

SiN