Java并发 专题
您的位置:java > Java并发专题 > Java并发AtomicReferenceArray类
Java并发AtomicReferenceArray类
作者:--    发布时间:2019-11-20

java.util.concurrent.atomic.atomicreferencearray类提供了可以原子读取和写入的底层引用数组的操作,并且还包含高级原子操作。 atomicreferencearray支持对底层引用数组变量的原子操作。 它具有获取和设置方法,如在变量上的读取和写入。 也就是说,一个集合与同一变量上的任何后续获取相关联。 原子compareandset方法也具有这些内存一致性功能。

atomicreferencearray方法

以下是atomicreferencearray类中可用的重要方法的列表。

序列 方法 描述
1 public boolean compareandset(int i, e expect, e update) 如果当前值==期望值,则将位置i处的元素原子设置为给定的更新值。
2 public e get(int i) 获取位置i的当前值。
3 public e getandset(int i, e newvalue) 将位置i处的元素原子设置为给定值,并返回旧值。
4 public void lazyset(int i, e newvalue) 最终将位置i处的元素设置为给定值。
5 public int length() 返回数组的长度。
6 public void set(int i, e newvalue) 将位置i处的元素设置为给定值。
7 public string tostring() 返回数组的当前值的string表示形式。
8 public boolean weakcompareandset(int i, e expect, e update) 如果当前值==期望值,则将位置i处的元素原子设置为给定的更新值。

实例

以下testthread程序显示了在线程环境中使用atomicreferencearray变量。

import java.util.concurrent.atomic.atomicreferencearray;

public class testthread {

   private static string[] source = new string[10];
   private static atomicreferencearray<string> atomicreferencearray 
      = new atomicreferencearray<string>(source);

   public static void main(final string[] arguments) throws interruptedexception {
      for (int i=0; i<atomicreferencearray.length(); i++) {
         atomicreferencearray.set(i, "item-2");
      }

      thread t1 = new thread(new increment());
      thread t2 = new thread(new compare());
      t1.start();
      t2.start();

      t1.join();
      t2.join();        
   }  

   static class increment implements runnable {
      public void run() {
         for(int i=0; i<atomicreferencearray.length(); i++) {
            string add = atomicreferencearray.getandset(i,"item-"+ (i+1));
            system.out.println("thread " + thread.currentthread().getid() 
               + ", index " +i + ", value: "+ add);
         }
      }
   }

   static class compare implements runnable {
      public void run() {
         for(int i=0; i<atomicreferencearray.length(); i++) {
            system.out.println("thread " + thread.currentthread().getid() 
               + ", index " +i + ", value: "+ atomicreferencearray.get(i));
            boolean swapped = atomicreferencearray.compareandset(i, "item-2", "updated-item-2");
            system.out.println("item swapped: " + swapped);
            if(swapped){
               system.out.println("thread " + thread.currentthread().getid() 
                  + ", index " +i + ", updated-item-2");
            }
         }
      }
   }
}

这将产生以下结果 -

thread 9, index 0, value: item-2
thread 10, index 0, value: item-1
item swapped: false
thread 10, index 1, value: item-2
item swapped: true
thread 9, index 1, value: updated-item-2
thread 10, index 1, updated-item-2
thread 10, index 2, value: item-3
item swapped: false
thread 10, index 3, value: item-2
item swapped: true
thread 10, index 3, updated-item-2
thread 10, index 4, value: item-2
item swapped: true
thread 10, index 4, updated-item-2
thread 10, index 5, value: item-2
item swapped: true
thread 10, index 5, updated-item-2
thread 10, index 6, value: item-2
thread 9, index 2, value: item-2
item swapped: true
thread 9, index 3, value: updated-item-2
thread 10, index 6, updated-item-2
thread 10, index 7, value: item-2
thread 9, index 4, value: updated-item-2
item swapped: true
thread 9, index 5, value: updated-item-2
thread 10, index 7, updated-item-2
thread 9, index 6, value: updated-item-2
thread 10, index 8, value: item-2
thread 9, index 7, value: updated-item-2
item swapped: true
thread 9, index 8, value: updated-item-2
thread 10, index 8, updated-item-2
thread 9, index 9, value: item-2
thread 10, index 9, value: item-10
item swapped: false

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册