AtomicIntegerArray就是采用的这种策略。 实现方式 由于java中的对象都是存放于堆中,而堆是线程公用的空间。而多线程的物理意义就是将计算机指令的复制,并用复制的代码的多核运行。 对于数组的原子性,AtomicIntegerArray首先在数组初始化的时候将数组的地址和数组元素的空间位数大小记录下来。 由于AtomicIntegerArray中并没有采用AtomicInteger,而是采用了基础数据类型Int,可能也是避免基础类型和对象的混用吧。 AtomicIntegerArray的主要接口 //进行CAS运算,判断第i个元素的expect是否与新的expect相同 public final boolean compareAndSet
和上一篇一样这几个类的使用方法几乎是一样的,所以我们以AtomicIntegerArray类来演示该原子类的使用,因为在上一篇中我们已经实验过了,这个包下的类确实是原子操作的,也就是说是线程安全的,所以下面的事例的重点在于演示方法的使用 4 AtomicIntegerArray类中的addAndGet()方法和AtomicInteger类中addAndGet()方法基本一样,唯一不同的就是多了一个数组的索引参数,操作时也就是通过操作的这个数组对象的索引值
为例csharp 体验AI代码助手 代码解读复制代码public static void main(String[] args) { AtomicIntegerArray atomicIntegerArray1 = new AtomicIntegerArray(5); AtomicIntegerArray atomicIntegerArray2 = new AtomicIntegerArray(new int[5]); AtomicIntegerArray atomicIntegerArray3 = new AtomicIntegerArray(new int[]{1, 2, 3, 4, 5}) (atomicIntegerArray3.get(i)); } int temp = 0; atomicIntegerArray3.getAndSet(0, 111); // 输出 111 System.out.println(atomicIntegerArray3.get(0)); // +1 atomicIntegerArray3.getAndIncrement
atomicIntegerArray = new AtomicIntegerArray(10); atomicIntegerArray.getAndIncrement(0); atomicIntegerArray.getAndAdd (1, 666); atomicIntegerArray.incrementAndGet(2); atomicIntegerArray.addAndGet(3, 666); atomicIntegerArray.compareAndSet (4, 0, 666); System.out.println(atomicIntegerArray.get(0)); System.out.println(atomicIntegerArray.get (1)); System.out.println(atomicIntegerArray.get(2)); System.out.println(atomicIntegerArray.get( 3)); System.out.println(atomicIntegerArray.get(4)); System.out.println(atomicIntegerArray.get(5
我们以AtomicIntegerArray为例来演示下用法 ? atomicIntegerArray = new AtomicIntegerArray(array); public static void main(String[] args) { // (0, 33)); // 44 // 输出 atomicIntegerArray中 log.info("atomicIntegerArray get :{}", atomicIntegerArray.get getAndAdd :{}", atomicIntegerArray.getAndAdd(0, 33)); // 44 log.info("atomicIntegerArray .get(0) : set,返回set之前的数据 log.info("atomicIntegerArray getAndSet :{}", atomicIntegerArray.getAndSet(0, 33));//
atomicIntegerArray = new AtomicIntegerArray(arr); //下标和步长 int i = atomicIntegerArray.addAndGet (1, 8); int j = atomicIntegerArray.addAndGet(0, 8); System.out.println("i:" + i + "- -j:" + j); System.out.println( atomicIntegerArray.toString()); //自定义运算 /** *下标 * 把下标的值更新为20 * 相关的运算 */ int k = atomicIntegerArray.accumulateAndGet left:right ); System.out.println( atomicIntegerArray.toString()); System.out.println
使用原子的方式更新数组里的某个元素 AtomicIntegerArray:整形数组原子类 AtomicLongArray:长整形数组原子类 AtomicReferenceArray :引用类型数组原子类 上面三个类提供的方法几乎相同,所以这里以 AtomicIntegerArray 为例子来介绍。 AtomicIntegerArray 类常用方法 public final int get(int i); // 获取 index=i 位置元素的值 public final int getAndSet 常见方法使用 import java.util.concurrent.atomic.AtomicIntegerArray; public class AtomicIntegerArrayTest { i = new AtomicIntegerArray(nums); for (int j = 0; j < nums.length; j++) { System.out.println
为此,Java并发包(JUC)提供了AtomicIntegerArray——一个基于CAS(Compare-And-Swap)机制的无锁原子整数数组类。 文章被收录于专栏:云时代Java开发:原理、实战与优化第一章:设计哲学——为何需要AtomicIntegerArray? 1.2AtomicIntegerArray的核心价值AtomicIntegerArray内部封装了一个普通的int[]数组,并通过底层的Unsafe或VarHandle直接对该数组的内存进行原子操作。 AtomicIntegerArray可以用来实现一个高效的、无锁的资源位图。 内存效率:在可能同时存在数百万个虚拟线程的系统中,AtomicIntegerArray的紧凑内存布局至关重要,能有效控制整体内存占用。
示例: public class AtomicIntegerArrayDemo { private static AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10); public static void main(final String[] arguments) throws InterruptedException { for (int i = 0; i < atomicIntegerArray.length(); i++) { atomicIntegerArray.set (); i++) { System.out.print(atomicIntegerArray.get(i) + " "); } } static (); i++) { boolean swapped = atomicIntegerArray.compareAndSet(i, 2, 3);
示例:AtomicIntegerArray 使用示例(AtomicLongArray 、AtomicReferenceArray 使用方式也类似) public class AtomicIntegerArrayDemo { private static AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(10); public (); i++) { atomicIntegerArray.set(i, i); System.out.print(atomicIntegerArray.get (); i++) { System.out.print(atomicIntegerArray.get(i) + " "); } System.out.println (); i++) { boolean swapped = atomicIntegerArray.compareAndSet(i, 2, 3);
AtomicIntegerArray:原子更新整型数组里的元素。 AtomicLongArray:原子更新长整型数组里的元素。 ---- 原子更新数组 AtomicIntegerArray:原子更新整型数组里的元素。 AtomicLongArray:原子更新长整型数组里的元素。 以上几个类提供的方法几乎一样,所以仅以 AtomicIntegerArray 为例进行介绍: AtomicIntegerArray 类主要是提供原子的方式更新数组里的整型。 ai = new AtomicIntegerArray(value); System.out.println("ai.getAndSet(0, 3)"); ai.getAndSet( 会将当前数组复制一份,所以当AtomicIntegerArray对内部的数组元素进行 修改 时,不会影响传入的数组。
AtomicIntegerArray(int length):创建一个给定长度的AtomicIntegerArray。 2. AtomicIntegerArray(int[] array):用一个给定的数组创建一个AtomicIntegerArray。 3. int length():返回AtomicIntegerArray的长度。 4. int get(int index):获取给定索引处的元素的值。 atomicIntegerArray = new AtomicIntegerArray(new int[5]); for (int i=0;i<atomicIntegerArray.length ();i++){ System.out.println(atomicIntegerArray.get(i)); } AtomicIntegerArray
原子更新数组类 通过原子的方式更新数组里的某个元素,Atomic包提供了以下三个类: AtomicIntegerArray:原子更新整型数组里的元素。 AtomicIntegerArray类主要是提供原子的方式更新数组里的整型,其常用方法如下 int addAndGet(int i, int delta):以原子方式将输入值与数组中索引i的元素相加。 ai = new AtomicIntegerArray(value); public static void main(String[] args) { ai.getAndSet System.out.println(ai.get(0)); System.out.println(value[0]); } } 输出 3 1 AtomicIntegerArray 类需要注意的是,数组value通过构造方法传递进去,然后AtomicIntegerArray会将当前数组复制一份,所以当AtomicIntegerArray对内部的数组元素进行修改时,不会影响到传入的数组
数组类型原子类 AtomicIntegerArray AtomicLongArray AtomicReferenceArray public class AtomicIntegerArrayDemo { public static void main(String[] args) { AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(new int[5]); //AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray (5); //AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(new int[]{1,2,3,4,5}); for (int i = 0; i <atomicIntegerArray.length(); i++) { System.out.println(atomicIntegerArray.get
atomicIntegerArray = new AtomicIntegerArray(1000); Incrementer incrementer = new Incrementer( atomicIntegerArray); Decrementer decrementer = new Decrementer(atomicIntegerArray); Thread (); i++) { if(atomicIntegerArray.get(i)! array; public Decrementer(AtomicIntegerArray array) { this.array = array; } @Override array; public Incrementer(AtomicIntegerArray array) { this.array = array; } @Override
原子类型数组有以下四种: AtomicIntegerArray AtomicLongArray AtomicReferenceArray AtomicBooleanArray AtomicIntegerArray public static void main(String[] args) { AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray (10); for (int i = 0; i < atomicIntegerArray.length(); i++) { // 这里执行,i的目标初始值是0,因为在for循环前定义了 i这个变量的值是0 // 第一个参数:更新的数组的索引;第二个参数:确认值;第三个参数:实行更改的值 boolean b = atomicIntegerArray.compareAndSet (atomicIntegerArray.get(2)); System.out.println(atomicIntegerArray.get(3)); } ///////////////////
数组类型 AtomicIntegerArray:整形数组原子类; AtomicLongArray:长整型数组原子类; AtomicReferenceArray:引用类型数组原子类。 AtomicIntegerArray示例 package cn.com.codingce.juc.atomic; import java.util.concurrent.atomic.AtomicIntegerArray ; public class AtomicIntegerArrayExample { private static AtomicIntegerArray atomicIntegerArray { System.out.println("Init Values: "); for (int i = 0; i < atomicIntegerArray.length (); i++) { atomicIntegerArray.set(i, i); System.out.print(atomicIntegerArray.get
static int[] value = new int[]{1,2}; static AtomicIntegerArray ai = new AtomicIntegerArray(value) AtomicIntegerArray会复制传递过来的数组,所以,AtomicIntegerArray修改内部数组元素时,不会影响传入的数组。 原子引用类型 3个原子引用类型,参加图中黄色框中的类。
当这类需求扩展到一组long值时,AtomicIntegerArray就显得力不从心了。 为此,Java并发包(JUC)提供了AtomicLongArray——AtomicIntegerArray的64位兄弟。 1.1AtomicIntegerArray的局限AtomicIntegerArray只能处理32位的int。 第二章:源码全景——64位原子性的实现奥秘2.1内部状态:原生long数组AtomicLongArray的结构与AtomicIntegerArray高度相似,但针对long类型进行了专门化。 2.3核心操作:带索引的64位CASAPI与AtomicIntegerArray几乎完全一致,只是类型从int变成了long。
) 我们首先来看一看AtomicIntegerArray的基本使用。 public class AtomicIntegerArray implements java.io.Serializable { ...... private final int[] array; ...... /** * Creates a new AtomicIntegerArray of the given * * @param length the length of the array */ public AtomicIntegerArray(int length 和上文中我们介绍的AtomicInteger类相似,AtomicIntegerArray中的decrementAndGet方法(还有其他操作方法)也是乐观锁的一个应用。