/** * Combine this reduction variable with the given value using the given operation. The result is * stored back into this reduction variable and is returned. * * @param value Value. * @param op Binary operation. * @return (This variable) <I>op</I> (<TT>value</TT>). */ public boolean reduce(boolean value, BooleanOp op) { for (; ; ) { boolean oldvalue = myValue.get(); boolean newvalue = op.op(oldvalue, value); if (myValue.compareAndSet(oldvalue, newvalue)) return newvalue; } }
/** * Combine this array reduction variable at the given index with the given value using the given * operation. (This array <TT>[i]</TT>) is set to (this array <TT>[i]</TT>) <I>op</I> * (<TT>value</TT>), then (this array <TT>[i]</TT>) is returned. * * @param i Index. * @param value Value. * @param op Binary operation. * @return (This array <TT>[i]</TT>) <I>op</I> (<TT>value</TT>). */ public boolean reduce(int i, boolean value, BooleanOp op) { for (; ; ) { int oldvalue = myArray.get(i); int newvalue = op.op(oldvalue != 0, value) ? 1 : 0; if (myArray.compareAndSet(i, oldvalue, newvalue)) { return newvalue != 0; } } }
/** * Combine a portion of this array reduction variable with a portion of the given array using the * given operation. For each index <TT>i</TT> from 0 to <TT>len</TT>-1, (this array * <TT>[dstoff+i]</TT>) is set to (this array <TT>[dstoff+i]</TT>) <I>op</I> * (<TT>src[srcoff+i]</TT>). * * <p>The <TT>reduce()</TT> method is multiple thread safe <I>on a per-element basis.</I> Each * individual array element is updated atomically, but the array as a whole is not updated * atomically. * * @param dstoff Index of first element to update in this array. * @param src Source array. * @param srcoff Index of first element to update from in the source array. * @param len Number of array elements to update. * @param op Binary operation. * @exception NullPointerException (unchecked exception) Thrown if <TT>src</TT> is null. Thrown if * <TT>op</TT> is null. * @exception IndexOutOfBoundsException (unchecked exception) Thrown if <TT>len</TT> < 0. * Thrown if any array index would be out of bounds. */ public void reduce(int dstoff, boolean[] src, int srcoff, int len, BooleanOp op) { if (len < 0 || dstoff < 0 || dstoff + len > myArray.length() || srcoff < 0 || srcoff + len > src.length) { throw new IndexOutOfBoundsException(); } while (len > 0) { updateLoop: for (; ; ) { int oldvalue = myArray.get(dstoff); int newvalue = op.op(oldvalue != 0, src[srcoff]) ? 1 : 0; if (myArray.compareAndSet(dstoff, oldvalue, newvalue)) break updateLoop; } ++dstoff; ++srcoff; --len; } }