Research on the usage of atomic type AtomicLong

AtomicLong​​​​Explore

​​​​ is​AtomicLong​ an atomic long integer class provided by Java, which provides atomic operations on long integer data. In a multi-threaded environment, ​AtomicLong​you can ensure that operations on long integer data are thread-safe.

In Android ​AtomicLong​, the usage and function of the ​AtomicLong​is the same as the standard Java. You can create a ​AtomicLong​object :

AtomicLong atomicLong = new AtomicLong(initialValue);

In the above code,​initialValue​​​ is ​AtomicLong​the initial value of the object.

Once ​AtomicLong​the object , you can manipulate it using the following methods:

  • ​​​​:​get()​ Get the current value.
  • ​​​​:​set(long newValue)​ Set a new value.
  • ​​​​:​getAndSet(long newValue)​ Get the current value and set a new value.
  • ​​​:​compareAndSet(long expect, long update)​ If the current value is equal to ​expect​​, set the value to ​update​​.
  • ​​​​:​getAndIncrement()​ Get the current value and increase it by 1.
  • ​​​​:​getAndDecrement()​ Get the current value and decrease it by 1.
  • ​​​​:​incrementAndGet()​ Increment the current value and get the new value.
  • ​​​​:​decrementAndGet()​ Decrement the current value and get the new value.

These methods provide atomic operations on ​AtomicLong​objects to ensure data security in a multi-threaded environment.

​​Android​AtomicLong​ code sample for :

import java.util.concurrent.atomic.AtomicLong;  
  
public class MyAtomicLong {  
    private static AtomicLong counter = new AtomicLong(0);  
  
    public static void main(String[] args) {  
        // 创建两个线程同时执行操作  
        Thread thread1 = new Thread(new MyRunnable());  
        Thread thread2 = new Thread(new MyRunnable());  
  
        thread1.start();  
        thread2.start();  
    }  
  
    private static class MyRunnable implements Runnable {  
        @Override  
        public void run() {  
            // 每次递增 1  
            long value = counter.incrementAndGet();  
            System.out.println("Value: " + value);  
        }  
    }  
}

In this example, we create an ​AtomicLong​object ​counter​and increment it using ​incrementAndGet()​the method . Then, we create two threads and start them, ​counter​operating . Due to the atomic operation ​AtomicLong​of , we can safely increment ​ in a multi-threaded environment ​counter​, avoiding the problem of race conditions.

The difference between AtomicLong and LongAddr

AtomicLong is based on the spin update of the CAS method; LongAdder divides the value into several cells. When the concurrency is low, the value is updated directly by CAS, and it ends when it succeeds.

In the case of high concurrency, CAS updates a certain cell value and expands the cell data if necessary, and ends successfully; if the update fails, spin CAS to update the cell value.

When taking the value, call the sum() method to accumulate each cell. AtomicLong contains APIs that combine atomic reading and writing; LongAdder does not have APIs that combine atomic reading and writing, which can guarantee the final consistency of results. The performance of AtomicLong and LongAdder in low concurrency scenarios is similar, and the performance of LongAdder in high concurrency scenarios is better than that of AtomicLong.

Guess you like

Origin blog.csdn.net/renhui1112/article/details/131871609