Quantcast
Channel: Can AtomicInteger replace synchronized? - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Can AtomicInteger replace synchronized?

$
0
0

The javadoc for the java.util.concurrent.atomic package says the following:

A small toolkit of classes that support lock-free thread-safe programming on single variables.

But I don't see any thread-safe (synchronized or Lock) code inside any of the AtomicInteger or AtomicBoolean classes.

So, are these 2 the same:

1.

int i;synchronized(this){i++;}

2.

AtomicInteger i = new AtomicInteger();i.getAndIncrement();

Update: Thanks for the answers. Is volatile needed when I use AtomicInteger?


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images