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

The following examples show how to use java.util.concurrent.atomic.AtomicIntegerArray#compareAndSet() . 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: ComposableFutures.java    From ob1k with Apache License 2.0 5 votes vote down vote up
/**
 * @param elements
 * @param rootNode:              The root of the tree currently being processed.
 * @param nodesState:            Pending node is marked with 0, processed node with 1.
 * @param results:               An array containing the results of the computation.
 * @param pendingNodeLowerBound: All elements with index < pendingNodeLowerBound are
 *                               either processed, or going to be processed by the thread modifying pendingNodeLowerBound.
 * @param producer
 * @param jumpWhenDone:          Indicates whether to continue processing from the pending node
 *                               with smallest index or return the processed results.
 * @param <T>
 * @param <R>
 * @return A ComposableFuture that will eventually apply producer to a subset of elements.
 * The computation progresses serially through a subset of elements in no particular order.
 * Since the algorithm is recursive, effort is made to limit the recursion depth.
 * The list of elements is treated as a binary tree rooted at index 1 (which maps to elements index 0),
 * where each tree rooted at index i has left subtree rooted at 2 * i and right subtree rooted
 * at 2 * i + 1.
 * Given an input rootNode, the subtree rooted at rootNode is traversed Pre-order serially.
 * When the traversal of a complete subtree ends,
 * or a processed element is encountered, the traversal proceeds
 * from the top leftmost pending node (smallest index).
 * This traversal strategy minimizes contention on the same part of the tree
 * by separate flows, and decreases the recursion depth.
 */
private static <T, R> ComposableFuture<Void> processTree(final List<T> elements,
                                                         final int rootNode,
                                                         final AtomicIntegerArray nodesState,
                                                         final R[] results,
                                                         final AtomicInteger pendingNodeLowerBound,
                                                         final FutureSuccessHandler<T, R> producer,
                                                         final boolean jumpWhenDone) {
  if (rootNode > elements.size()) {
    return ComposableFutures.fromNull();
  }

  if (!nodesState.compareAndSet(rootNode - 1, 0, 1)) {
    // A parallel flow is working on our subtree, let's look for another subtree.
    return jump(elements, nodesState, results, pendingNodeLowerBound, producer);
  }

  final ComposableFuture<R> root = producer.handle(elements.get(rootNode - 1));

  return root.flatMap(rootResult -> {
    results[rootNode - 1] = rootResult;
    final int leftNode = rootNode << 1;
    final ComposableFuture<Void> left = processTree(elements, leftNode, nodesState, results, pendingNodeLowerBound, producer, false);
    return left.flatMap(leftResults -> {
      final int rightNode = leftNode + 1;
      final ComposableFuture<Void> right = processTree(elements, rightNode, nodesState, results, pendingNodeLowerBound, producer, false);
      return jumpWhenDone ? right.flatMap(rightResults -> jump(elements, nodesState, results, pendingNodeLowerBound, producer)) : right;
    });
  });
}
 
Example 2
Source File: TestIntAtomicCAS.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static void test_2ci_inv(AtomicIntegerArray a, AtomicIntegerArray b, int k) {
  for (int i = 0; i < ARRLEN-k; i+=1) {
    a.compareAndSet((i+k), 123, -123);
    b.compareAndSet((i+k), 123, -103);
  }
}
 
Example 3
Source File: TestIntAtomicCAS.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void test_ci_off(AtomicIntegerArray a, int old) {
  for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
    a.compareAndSet((i+OFFSET), old, -123);
  }
}
 
Example 4
Source File: TestIntAtomicCAS.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static void test_cp_inv(AtomicIntegerArray a, AtomicIntegerArray b, int k) {
  for (int i = 0; i < ARRLEN-k; i+=1) {
    a.compareAndSet((i+k), -123, b.get(i+k));
  }
}
 
Example 5
Source File: TestIntAtomicCAS.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static void test_2ci_unaln(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = 0; i < ARRLEN-UNALIGN_OFF; i+=1) {
    a.compareAndSet((i+UNALIGN_OFF), -1, -123);
    b.getAndSet(i, -103);
  }
}
 
Example 6
Source File: TestIntAtomicCAS.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static void test_ci_scl(AtomicIntegerArray a, int old) {
  for (int i = 0; i*SCALE < ARRLEN; i+=1) {
    a.compareAndSet((i*SCALE), old, -123);
  }
}
 
Example 7
Source File: TestIntAtomicCAS.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
static void test_ci(AtomicIntegerArray a) {
  for (int i = 0; i < ARRLEN; i+=1) {
    a.compareAndSet(i, -1, -123);
  }
}
 
Example 8
Source File: TestIntAtomicCAS.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static void test_vi_off(AtomicIntegerArray a, int b, int old) {
  for (int i = 0; i < ARRLEN-OFFSET; i+=1) {
    a.compareAndSet((i+OFFSET), old, b);
  }
}
 
Example 9
Source File: TestIntAtomicCAS.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static void test_cp_neg(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, -123, b.get(i));
  }
}
 
Example 10
Source File: TestIntAtomicCAS.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static void test_cp_neg(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, -123, b.get(i));
  }
}
 
Example 11
Source File: TestIntAtomicCAS.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
static void test_2vi_neg(AtomicIntegerArray a, AtomicIntegerArray b, int c, int d) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, -123, c);
    b.compareAndSet(i, -103, d);
  }
}
 
Example 12
Source File: TestIntAtomicCAS.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
static void test_cp(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = 0; i < ARRLEN; i+=1) {
    a.compareAndSet(i, -123, b.get(i));
  }
}
 
Example 13
Source File: TestIntAtomicCAS.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void test_cp_neg(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, -123, b.get(i));
  }
}
 
Example 14
Source File: TestIntAtomicCAS.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static void test_2ci_neg(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, 123, -123);
    b.compareAndSet(i, 123, -103);
  }
}
 
Example 15
Source File: TestIntAtomicCAS.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void test_vi_inv(AtomicIntegerArray a, int b, int k, int old) {
  for (int i = 0; i < ARRLEN-k; i+=1) {
    a.compareAndSet((i+k), old, b);
  }
}
 
Example 16
Source File: TestIntAtomicCAS.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
static void test_cp_scl(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = 0; i*SCALE < ARRLEN; i+=1) {
    a.compareAndSet((i*SCALE), -123, b.get(i*SCALE));
  }
}
 
Example 17
Source File: TestIntAtomicCAS.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
static void test_ci_scl(AtomicIntegerArray a, int old) {
  for (int i = 0; i*SCALE < ARRLEN; i+=1) {
    a.compareAndSet((i*SCALE), old, -123);
  }
}
 
Example 18
Source File: TestIntAtomicCAS.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static void test_2ci(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = 0; i < ARRLEN; i+=1) {
    a.compareAndSet(i, 123, -123);
    b.compareAndSet(i, 123, -103);
  }
}
 
Example 19
Source File: TestIntAtomicCAS.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static void test_2ci_neg(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, 123, -123);
    b.compareAndSet(i, 123, -103);
  }
}
 
Example 20
Source File: TestIntAtomicCAS.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
static void test_2ci_neg(AtomicIntegerArray a, AtomicIntegerArray b) {
  for (int i = ARRLEN-1; i >= 0; i-=1) {
    a.compareAndSet(i, 123, -123);
    b.compareAndSet(i, 123, -103);
  }
}