This is the 9th day of my participation in Gwen Challenge

Author: JavaGieGie

Wechat official account: Java development zero to one

preface

AtomicInteger is Java. The util. Concurrent. An atomic classes under the atomic package, the package AtomicBoolean, are still under the AtomicLong, AtomicLongArray, AtomicReference atomic classes, It is used to ensure thread safety in a high concurrency environment.

The body of the

1. Application scenarios

As we all know, a++ is not thread-safe in the case of multiple threads executing concurrently. And because the a++ process consists of three steps that are non-atomic, even volatile is not thread-safe; Locking (such as Synchronized) is very performance critical, so AtomicInteger is a good choice for variable increment.

2. Case code

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerDemo implements Runnable {
    private static final AtomicInteger atomicInteger = new AtomicInteger();

    // Increase the specified number
    public void getAndAdd(a) {
        atomicInteger.getAndAdd(-90);
    }
    //增加1
    public void getAndIncrement(a) {
        atomicInteger.getAndIncrement();
    }
    / / reduced by 1
    public void getAndDecrement(a) {
        atomicInteger.getAndDecrement();
    }
    public static void main(String[] args) throws InterruptedException {
        AtomicIntegerDemo r = new AtomicIntegerDemo();
        Thread t1 = new Thread(r);
        t1.start();
        t1.join();
        System.out.println("AtomicInteger operation result:" + atomicInteger.get());
    }

    @Override
    public void run(a) {
        for (int i = 0; i < 10000; i++) { getAndDecrement(); }}}Copy the code

3. Method introduction

  1. getAndIncrement

Perform the +1 operation on the variable atomicInteger.

  1. getAndAdd

The variable atomicInteger can be incremented by N (specifying an incremented amount).

  1. getAndDecrement

Perform the -1 operation on the variable atomicInteger.

  1. get

Gets the current AtomicInteger value.

4. Introduction to the source code

Here introduce AtomicInteger. IncrementAndGet () method, this method has an infinite loop, it will first get the current value, and then call compareAndSet method, judge whether the current value has been modified by another thread, If compareAndSet returns false, it will continue to retry until it succeeds, which is the essence of AtomicInteger’s ability to achieve atomicity.

public final int incrementAndGet(a) {
    for (;;) {
        int current = get();
        int next = current + 1;
        if (compareAndSet(current, next))
            returnnext; }}Copy the code

CompareAndSet: compareAndSet

public final boolean compareAndSet(int expect, int update) {
    return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
Copy the code

CompareAndSet () calls the Unsafe.compareAndSwapInt() method, also known as the CAS operation of the Unsafe class.

CAS will explain in the series “Squat can enter dachang”, interested partners can pay attention to.

conclusion

AtomicInteger application scenarios are also relatively simple, in addition to the highly competitive situation, atomic class compared to the ordinary lock, granular and more efficient, this article introduces the common methods, there is a need for in-depth understanding of the source code, can continue to explore, have any questions can be in the article below the comment.

Pay attention to avoid getting lost

The above is the whole content of this issue, if there is any mistake, please leave a message for advice, thank you very much. I’m GieGie, feel free to leave a comment and we’ll see you next time at 🦮.

The article continues to update, you can wechat search Java development zero to one for the first time to read, and you can get interview materials learning video, interested partners welcome to pay attention to, learn together, ha 🐮🥃.

Original is not easy, how can you bear to whoring for nothing, if you think this article is a little useful to you, thank old tie for this article to like, comment or forward, because this will be my output more quality articles of power, thank you!