Java Code Examples for java.util.concurrent.atomic.AtomicIntegerArray#length()

The following examples show how to use java.util.concurrent.atomic.AtomicIntegerArray#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtomicIntegerArrayDemo.java    From rome with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    int temValue = 0;

    int[] items = {1,2,3,4,5,6,7,8,9,10};
    AtomicIntegerArray array = new AtomicIntegerArray(items);

    for (int i = 0; i < array.length(); i++) {
        System.out.println(array.get(i));
    }

    temValue = array.getAndSet(0, 11);
    System.out.println("temvalue:" + temValue + ";  array:" + array);

    temValue = array.getAndIncrement(1);
    System.out.println("temvalue:" + temValue + ";  array:" + array);

    array.getAndAdd(0, 10);
    System.out.println("temvalue:" + temValue + ";  array:" + array);
}
 
Example 2
Source File: MemoryChunkJUnitTestBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
static private ArrayList<Bucket> computeHistogram(AtomicIntegerArray originalValues, final int granualarity) {
  int[] values = new int[originalValues.length()];
  for (int i=0; i < values.length; i++) {
    values[i] = originalValues.get(i);
  }
  Arrays.sort(values);
  ArrayList<Bucket> result = new ArrayList<Bucket>();
  Bucket curBucket = new Bucket(values[0]);
  result.add(curBucket);
  for (int i=1; i < values.length; i++) {
    int curVal = values[i];
    if (!curBucket.addValue(curVal, granualarity)) {
      curBucket = new Bucket(curVal);
      result.add(curBucket);
    }
  }
  return result;
}
 
Example 3
Source File: MemoryChunkJUnitTestBase.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
static private ArrayList<Bucket> computeHistogram(AtomicIntegerArray originalValues, final int granualarity) {
  int[] values = new int[originalValues.length()];
  for (int i=0; i < values.length; i++) {
    values[i] = originalValues.get(i);
  }
  Arrays.sort(values);
  ArrayList<Bucket> result = new ArrayList<Bucket>();
  Bucket curBucket = new Bucket(values[0]);
  result.add(curBucket);
  for (int i=1; i < values.length; i++) {
    int curVal = values[i];
    if (!curBucket.addValue(curVal, granualarity)) {
      curBucket = new Bucket(curVal);
      result.add(curBucket);
    }
  }
  return result;
}
 
Example 4
Source File: ChoiceOfTwoLoadBalancerTest.java    From ocelli with Apache License 2.0 6 votes vote down vote up
@Test
public void testMany() {
    BehaviorSubject<List<Integer>> source = BehaviorSubject.create();
    LoadBalancer<Integer> lb = LoadBalancer.fromSnapshotSource(source).build(ChoiceOfTwoLoadBalancer.create(COMPARATOR));
    
    source.onNext(Lists.newArrayList(0,1,2,3,4,5,6,7,8,9));
    
    AtomicIntegerArray counts = new AtomicIntegerArray(10);
    
    for (int i = 0; i < 100000; i++) {
        counts.incrementAndGet(lb.next());
    }
    Double[] pct = new Double[counts.length()];
    for (int i = 0; i < counts.length(); i++) {
        pct[i] = counts.get(i)/100000.0;
    }
    
    for (int i = 1; i < counts.length(); i++) {
        Assert.assertTrue(counts.get(i) > counts.get(i-1));
    }
}
 
Example 5
Source File: Main.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {

        System.out.println("updateAndGet(), accumulateAndGet():");
        AtomicIntegerArray atomicArray = new AtomicIntegerArray(new int[]{3, 4, 2, 5});
        for (int i = 0; i < atomicArray.length(); i++) {
            atomicArray.updateAndGet(i, elem -> elem * elem);
        }
        System.out.println("Result: " + atomicArray);

        AtomicInteger nr1 = new AtomicInteger(3);
        int result1 = nr1.accumulateAndGet(5, (x, y) -> x * y); // x = 3, y = 5

        AtomicInteger nr2 = new AtomicInteger(3);
        int result2 = nr2.updateAndGet(x -> 5 * x);

        System.out.println("Result (nr1): " + result1);
        System.out.println("Result (nr2): " + result2);

        System.out.println("\naddAndGet():");
        AtomicInteger nr3 = new AtomicInteger(3);
        int result3 = nr3.addAndGet(4);
        System.out.println("Result (nr3): " + result3);

        System.out.println("\ncompareAndSet():");
        AtomicInteger nr4 = new AtomicInteger(3);
        boolean wasSet = nr4.compareAndSet(3, 5);
        System.out.println("Result (nr4): " + nr4.get() + " Was set: " + wasSet);
    }
 
Example 6
Source File: TypeAdapters.java    From GVGAI_GYM with Apache License 2.0 5 votes vote down vote up
@Override public void write(JsonWriter out, AtomicIntegerArray value) throws IOException {
  out.beginArray();
  for (int i = 0, length = value.length(); i < length; i++) {
    out.value(value.get(i));
  }
  out.endArray();
}
 
Example 7
Source File: TypeAdapters.java    From GVGAI_GYM with Apache License 2.0 5 votes vote down vote up
@Override public void write(JsonWriter out, AtomicIntegerArray value) throws IOException {
  out.beginArray();
  for (int i = 0, length = value.length(); i < length; i++) {
    out.value(value.get(i));
  }
  out.endArray();
}
 
Example 8
Source File: TypeAdapters.java    From sagetv with Apache License 2.0 5 votes vote down vote up
@Override public void write(JsonWriter out, AtomicIntegerArray value) throws IOException {
  out.beginArray();
  for (int i = 0, length = value.length(); i < length; i++) {
    out.value(value.get(i));
  }
  out.endArray();
}
 
Example 9
Source File: TypeAdapters.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override public void write(JsonWriter out, AtomicIntegerArray value) throws IOException {
  out.beginArray();
  for (int i = 0, length = value.length(); i < length; i++) {
    out.value(value.get(i));
  }
  out.endArray();
}
 
Example 10
Source File: TestNMClientAsync.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void assertAtomicIntegerArray(AtomicIntegerArray array) {
  for (int i = 0; i < array.length(); ++i) {
    Assert.assertEquals(1, array.get(i));
  }
}
 
Example 11
Source File: DiffEqualityTypeMapper.java    From hollow with Apache License 2.0 4 votes vote down vote up
protected int[] hashToOrdinals() {
    PopulatedOrdinalListener listener = toState.getListener(PopulatedOrdinalListener.class);
    final BitSet toPopulatedOrdinals = listener.getPopulatedOrdinals();
    final int ordinalSpaceLength = toPopulatedOrdinals.length();

    int hashedOrdinalsLength = 1 << (32 - Integer.numberOfLeadingZeros((toPopulatedOrdinals.cardinality() * 2) - 1));

    final AtomicIntegerArray hashedToOrdinals = new AtomicIntegerArray(hashedOrdinalsLength);
    for(int i=0;i<hashedOrdinalsLength;i++)
        hashedToOrdinals.set(i, -1);

    SimultaneousExecutor executor = new SimultaneousExecutor(1.5d, getClass(), "hash-to-ordinals");
    final int numThreads = executor.getCorePoolSize();

    for(int i=0;i<numThreads;i++) {
        final int threadNumber = i;

        executor.execute(() -> {
            for(int t=threadNumber;t<ordinalSpaceLength;t+=numThreads) {
                if(toPopulatedOrdinals.get(t)) {
                    int hashCode = toRecordHashCode(t);
                    if(hashCode != -1) {
                        int bucket = hashCode & (hashedToOrdinals.length() - 1);
                        while(!hashedToOrdinals.compareAndSet(bucket, -1, t)) {
                            bucket = (bucket + 1) & (hashedToOrdinals.length() - 1);
                        }
                    }
                }
            }
        });
    }

    try {
        executor.awaitSuccessfulCompletion();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }

    int arr[] = new int[hashedToOrdinals.length()];
    for(int i=0;i<arr.length;i++) {
        arr[i] = hashedToOrdinals.get(i);
    }
    return arr;
}
 
Example 12
Source File: IntegerStatsDeltaAggregator.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void initializeArray(AtomicIntegerArray arr){
  for(int i = 0; i<arr.length() ; i++){
    arr.set(i, Integer.valueOf(0));
  }
}
 
Example 13
Source File: TestNMClientAsync.java    From big-c with Apache License 2.0 4 votes vote down vote up
private void assertAtomicIntegerArray(AtomicIntegerArray array) {
  for (int i = 0; i < array.length(); ++i) {
    Assert.assertEquals(1, array.get(i));
  }
}
 
Example 14
Source File: IntegerStatsDeltaAggregator.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
private void initializeArray(AtomicIntegerArray arr){
  for(int i = 0; i<arr.length() ; i++){
    arr.set(i, Integer.valueOf(0));
  }
}