Can AtomicInteger replace synchronized?

后端 未结 5 969
不思量自难忘°
不思量自难忘° 2020-12-31 02:21

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

A small toolkit of classes that support lock-free thread-safe programm

5条回答
  •  醉梦人生
    2020-12-31 02:39

    They are equivalent functionally, but there is a subtle difference. synchronized has the overhead of acquiring and releasing the monitor on this, while AtomicInteger is implemented with a native method call, so it will be significantly faster.

提交回复
热议问题