Java 多线程编程之原子类 AtomicIntegerArray(构造方法、获取与设置方法、比较并设置方法、比较并交换方法、自增自减方法、获取并累加方法、进阶操作方法)

一、AtomicIntegerArray

  • AtomicIntegerArray 是 Java 并发包 (java.util.concurrent.atomic) 中的一个类,它提供了一种线程安全的整数数组
  1. AtomicIntegerArray 保证操作原子性

  2. AtomicIntegerArray 保证操作可见性

  3. AtomicIntegerArray 创建后长度不可变


二、构造方法

1、基本介绍
AtomicIntegerArray(int length)
  1. 创建给定长度的新 AtomicIntegerArray,所有元素初始为 0
AtomicIntegerArray(int[] array)
  1. 创建与给定数组相同长度的新 AtomicIntegerArray,并拷贝所有元素
2、演示
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10);

System.out.println(atomicIntegerArray.length());
System.out.println(atomicIntegerArray);
# 输出结果

10
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

System.out.println(atomicIntegerArray.length());
System.out.println(atomicIntegerArray);
# 输出结果

3
[1, 2, 3]

三、获取与设置方法

1、基本介绍
int get(int i)
  1. 返回索引 i 处的当前值
void set(int i, int newValue)
  1. 将索引 i 处的值设置为 newValue
int getAndSet(int i, int newValue)
  1. 将索引 i 处的值设置为 newValue,并返回旧值
2、演示
  1. get 方法与 set 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

System.out.println(atomicIntegerArray.get(0));
System.out.println(atomicIntegerArray.get(1));
System.out.println(atomicIntegerArray.get(2));

atomicIntegerArray.set(0, 10);
atomicIntegerArray.set(1, 20);
atomicIntegerArray.set(2, 30);

System.out.println(atomicIntegerArray.get(0));
System.out.println(atomicIntegerArray.get(1));
System.out.println(atomicIntegerArray.get(2));
# 输出结果

1
2
3
10
20
30
  1. getAndSet 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result1 = atomicIntegerArray.getAndSet(0, 10);
int result2 = atomicIntegerArray.getAndSet(1, 20);
int result3 = atomicIntegerArray.getAndSet(2, 30);

System.out.println(result1);
System.out.println(result2);
System.out.println(result3);

System.out.println(atomicIntegerArray.get(0));
System.out.println(atomicIntegerArray.get(1));
System.out.println(atomicIntegerArray.get(2));
# 输出结果

1
2
3
10
20
30

四、比较并设置方法

1、基本介绍
boolean compareAndSet(int i, int expectedValue, int newValue)
  1. 如果当前索引 i 处的值等于 expectedValue,则将其设置为 newValue,并返回 true

  2. 否则不进行任何操作,并返回 false

2、演示
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

boolean result1 = atomicIntegerArray.compareAndSet(0, 10, 100);

System.out.println(result1);
System.out.println(atomicIntegerArray.get(0));

boolean result2 = atomicIntegerArray.compareAndSet(0, 1, 100);

System.out.println(result2);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

false
1
true
100

五、比较并交换方法

1、基本介绍
int compareAndExchange(int i, int expectedValue, int newValue)
  1. 如果当前索引 i 处的值等于 expectedValue,则将其设置为 newValue,并返回旧值

  2. 否则不进行任何操作,并返回当前索引 i 处的值

2、演示
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result1 = atomicIntegerArray.compareAndExchange(0, 10, 100);

System.out.println(result1);
System.out.println(atomicIntegerArray.get(0));

int result2 = atomicIntegerArray.compareAndExchange(0, 1, 100);

System.out.println(result2);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

1
1
1
100

六、自增自减方法

1、基本介绍
int incrementAndGet(int i)
  1. 将索引 i 处的值加 1,并返回新值
int getAndIncrement(int i)
  1. 将索引 i 处的值加 1,并返回旧值
int decrementAndGet(int i)
  1. 将索引 i 处的值减 1,并返回新值
int getAndDecrement(int i)
  1. 将索引 i 处的值减 1,并返回旧值
2、演示
  1. incrementAndGet 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.incrementAndGet(0);

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

2
2
  1. getAndIncrement 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.getAndIncrement(0);

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

1
2
  1. decrementAndGet 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.decrementAndGet(0);

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

0
0
  1. getAndDecrement 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.getAndDecrement(0);

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

1
0

七、获取并累加方法

1、基本介绍
int addAndGet(int i, int delta)
  1. 将索引 i 处的值加上 delta,并返回新值
int getAndSet(int i, int newValue)
  1. 将索引 i 处的值加上 delta,并返回旧值
2、演示
  1. addAndGet 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result1 = atomicIntegerArray.addAndGet(0, 10);

System.out.println(result1);
System.out.println(atomicIntegerArray.get(0));

int result2 = atomicIntegerArray.addAndGet(0, -10);

System.out.println(result2);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

11
11
1
1
  1. getAndAdd 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result1 = atomicIntegerArray.getAndSet(0, 10);

System.out.println(result1);
System.out.println(atomicIntegerArray.get(0));

int result2 = atomicIntegerArray.getAndSet(0, -10);

System.out.println(result2);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

1
10
10
-10

八、进阶操作方法

1、基本介绍
int getAndUpdate(int i, IntUnaryOperator updateFunction)
  1. 将索引 i 处的值进行 updateFunction 操作,并返回旧值
int updateAndGet(int i, IntUnaryOperator updateFunction)
  1. 将索引 i 处的值进行 updateFunction 操作,并返回新值
int getAndAccumulate(int i, int x, IntBinaryOperator accumulatorFunction)
  1. 将索引 i 处的值与 x 进行 accumulatorFunction 操作,并返回旧值
int accumulateAndGet(int i, int x, IntBinaryOperator accumulatorFunction)
  1. 将索引 i 处的值与 x 进行 accumulatorFunction 操作,并返回新值
2、演示
  1. getAndUpdate 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.getAndUpdate(0, (value) -> {
    System.out.println("value: " + value);
    return value + 10;
});

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

value: 1
1
11
  1. updateAndGet 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.updateAndGet(0, (value) -> {
    System.out.println("value: " + value);
    return value + 10;
});

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

value: 1
11
11
  1. getAndAccumulate 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.getAndAccumulate(0, 10, (value, x) -> {
    System.out.println("value: " + value);
    System.out.println("x: " + x);
    return value + x;
});

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

value: 1
x: 10
1
11
  1. accumulateAndGet 方法
int[] array = {1, 2, 3};
AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(array);

int result = atomicIntegerArray.accumulateAndGet(0, 10, (value, x) -> {
    System.out.println("value: " + value);
    System.out.println("x: " + x);
    return value + x;
});

System.out.println(result);
System.out.println(atomicIntegerArray.get(0));
# 输出结果

value: 1
x: 10
11
11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值