14 AtomicInteger

jdk 基于 8 版本

在平时的开发中,我们经常会用到 AtomicInteger, 它是原子计数,与之类似的还有很多,比如 AtomicBoolean, AtomicLong, AtomicReferenceFieldUpdater

使用方式

1
2
3
4
5
6
7
8
9
public class AtomicIntegerTest {

    @Test
    void test() {
        AtomicInteger atomicInteger = new AtomicInteger();
        assertThat(atomicInteger.get()).isEqualTo(0);
        assertThat(atomicInteger.incrementAndGet()).isEqualTo(1);
    }
}

get

获取值。

incrementAndGet

加 1 后获取值。

0%