Can synchronized blocks be faster than Atomics?

后端 未结 5 1834
天命终不由人
天命终不由人 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:57

    incrementAndGet may well be implemented as a CAS-loop. In highly contented situations that can result in n-1 of your threads failing leading to an O(n) problem, for n threads.

    ( For @Geek:

    Typically getAndIncrement may be implemented something like:

     int old;
     do {
         old = value;
     } while (!compareAndSet(value, old, old+1));
     return old;
    

    Imagine you have a n threads executing this code on the same atomic, and they happen to be in step with each other. The first iteration does kn work. Only one CAS will succeed. The other n-1 threads will repeat the exercise until there is only one left. So the total work is O(n^2) (worst case) instead of O(n). )

    Having said that, acquiring a lock will need to do something similar at best, and locks aren't at their best when heavily contended. You're not likely to see much of an advantage for locks until you use a CAS-loop which requires significant computation before get and compareAndSwap.

提交回复
热议问题