Hossein
Hossein

Reputation: 41841

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

Upvotes: 10

Views: 3599

Answers (1)

SiN
SiN

Reputation: 3754

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

Upvotes: 14

Related Questions