Exemplo n.º 1
0
  public static void main(String args[]) {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

    // Predicate<Integer> predicate = n -> true
    // n 是一个参数传递到 Predicate 接口的 test 方法
    // n 如果存在则 test 方法返回 true

    System.out.println("输出所有数据:");

    // 传递参数 n
    eval(list, n -> true);

    // Predicate<Integer> predicate1 = n -> n%2 == 0
    // n 是一个参数传递到 Predicate 接口的 test 方法
    // 如果 n%2 为 0 test 方法返回 true

    System.out.println("输出所有偶数:");
    eval(list, n -> n % 2 == 0);

    // Predicate<Integer> predicate2 = n -> n > 3
    // n 是一个参数传递到 Predicate 接口的 test 方法
    // 如果 n 大于 3 test 方法返回 true

    System.out.println("输出大于 3 的所有数字:");
    eval(list, n -> n > 3);

    LongBinaryOperator longBinOp = (a, b) -> a * b;
    long result = longBinOp.applyAsLong(4, 5);
    System.out.println(result);
  }
Exemplo n.º 2
0
 /**
  * Updates with the given value.
  *
  * @param x the value
  */
 public void accumulate(long x) {
   Cell[] as;
   long b, v, r;
   int m;
   Cell a;
   if ((as = cells) != null || (r = function.applyAsLong(b = base, x)) != b && !casBase(b, r)) {
     boolean uncontended = true;
     if (as == null
         || (m = as.length - 1) < 0
         || (a = as[getProbe() & m]) == null
         || !(uncontended = (r = function.applyAsLong(v = a.value, x)) == v || a.cas(v, r)))
       longAccumulate(x, function, uncontended);
   }
 }
 @Override
 public long reduce(long identity, LongBinaryOperator op) {
   return performOperation(
       TerminalFunctions.reduceFunction(identity, op),
       true,
       (i1, i2) -> op.applyAsLong(i1, i2),
       null);
 }
Exemplo n.º 4
0
 /**
  * Atomically updates the element at index {@code i} with the results of applying the given
  * function to the current and given values, returning the updated value. The function should be
  * side-effect-free, since it may be re-applied when attempted updates fail due to contention
  * among threads. The function is applied with the current value at index {@code i} as its first
  * argument, and the given update as the second argument.
  *
  * @param i the index
  * @param x the update value
  * @param accumulatorFunction a side-effect-free function of two arguments
  * @return the updated value
  * @since 1.8
  */
 public final long accumulateAndGet(int i, int x, LongBinaryOperator accumulatorFunction) {
   long offset = checkedByteOffset(i);
   long prev, next;
   do {
     prev = getRaw(offset);
     next = accumulatorFunction.applyAsLong(prev, x);
   } while (!compareAndSetRaw(offset, prev, next));
   return next;
 }
Exemplo n.º 5
0
 /**
  * Returns the current value. The returned value is <em>NOT</em> an atomic snapshot; invocation in
  * the absence of concurrent updates returns an accurate result, but concurrent updates that occur
  * while the value is being calculated might not be incorporated.
  *
  * @return the current value
  */
 public long get() {
   Cell[] as = cells;
   Cell a;
   long result = base;
   if (as != null) {
     for (int i = 0; i < as.length; ++i) {
       if ((a = as[i]) != null) result = function.applyAsLong(result, a.value);
     }
   }
   return result;
 }
Exemplo n.º 6
0
 /**
  * Equivalent in effect to {@link #get} followed by {@link #reset}. This method may apply for
  * example during quiescent points between multithreaded computations. If there are updates
  * concurrent with this method, the returned value is <em>not</em> guaranteed to be the final
  * value occurring before the reset.
  *
  * @return the value before reset
  */
 public long getThenReset() {
   Cell[] as = cells;
   Cell a;
   long result = base;
   base = identity;
   if (as != null) {
     for (int i = 0; i < as.length; ++i) {
       if ((a = as[i]) != null) {
         long v = a.value;
         a.value = identity;
         result = function.applyAsLong(result, v);
       }
     }
   }
   return result;
 }
 @Override
 public OptionalLong reduce(LongBinaryOperator op) {
   Long result =
       performOperation(
           TerminalFunctions.reduceFunction(op),
           true,
           (i1, i2) -> {
             if (i1 != null) {
               if (i2 != null) {
                 return op.applyAsLong(i1, i2);
               }
               return i1;
             }
             return i2;
           },
           null);
   if (result == null) {
     return OptionalLong.empty();
   } else {
     return OptionalLong.of(result);
   }
 }