Can synchronized blocks be faster than Atomics?

后端 未结 5 1810
天命终不由人
天命终不由人 2021-02-15 15:10

Suppose two following counter implementations:

class Counter {
  private final AtomicInteger atomic = new AtomicInteger(0);
  private int i = 0;

  public void i         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-15 15:38

    atomic variables will always be faster.

    you can see that the java.util.concurrent package always utilises atomic variables rather than synchronized blocks.

提交回复
热议问题