Search in sources :

Example 26 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project eclipse-collections by eclipse.

the class ConcurrentHashMap method resize.

// newSize must be a power of 2 + 1
@SuppressWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER")
private void resize(AtomicReferenceArray oldTable, int newSize) {
    int oldCapacity = oldTable.length();
    int end = oldCapacity - 1;
    Object last = oldTable.get(end);
    if (this.size() < end && last == RESIZE_SENTINEL) {
        return;
    }
    if (oldCapacity >= MAXIMUM_CAPACITY) {
        throw new RuntimeException("index is too large!");
    }
    ResizeContainer resizeContainer = null;
    boolean ownResize = false;
    if (last == null || last == RESIZE_SENTINEL) {
        synchronized (// allocating a new array is too expensive to make this an atomic operation
        oldTable) {
            if (oldTable.get(end) == null) {
                oldTable.set(end, RESIZE_SENTINEL);
                if (this.partitionedSize == null && newSize >= PARTITIONED_SIZE_THRESHOLD) {
                    this.partitionedSize = new AtomicIntegerArray(SIZE_BUCKETS * 16);
                }
                resizeContainer = new ResizeContainer(new AtomicReferenceArray(newSize), oldTable.length() - 1);
                oldTable.set(end, resizeContainer);
                ownResize = true;
            }
        }
    }
    if (ownResize) {
        this.transfer(oldTable, resizeContainer);
        AtomicReferenceArray src = this.table;
        while (!TABLE_UPDATER.compareAndSet(this, oldTable, resizeContainer.nextArray)) {
            // we're in a double resize situation; we'll have to go help until it's our turn to set the table
            if (src != oldTable) {
                this.helpWithResize(src);
            }
        }
    } else {
        this.helpWithResize(oldTable);
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray)

Example 27 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project ffx by mjschnie.

the class SharedIntegerMatrix method reduce.

/**
 * Combine a portion of this matrix reduction variable with a portion of the
 * given matrix using the given operation. For each row index <TT>r</TT>
 * from 0 to <TT>rowlen-1</TT> inclusive, and for each column index
 * <TT>c</TT> from 0 to <TT>collen-1</TT> inclusive, (this matrix
 * <TT>[dstrow+r,dstcol+c]</TT>) is set to (this matrix
 * <TT>[dstrow+r,dstcol+c]</TT>) <I>op</I>
 * (<TT>src[srcrow+r,srccol+c]</TT>).
 * <P>
 * The <TT>reduce()</TT> method is multiple thread safe <I>on a per-element
 * basis.</I> Each individual matrix element is updated atomically, but the
 * matrix as a whole is not updated atomically.
 *
 * @param dstrow Row index of first element to update in this matrix.
 * @param dstcol Column index of first element to update in this matrix.
 * @param src Source matrix.
 * @param srcrow Row index of first element to update from in the source
 * matrix.
 * @param srccol Column index of first element to update from in the source
 * matrix.
 * @param srcrow Row index of first element to update from in the source
 * matrix.
 * @param srccol Column index of first element to update from in the source
 * matrix.
 * @param rowlen Number of rows to update.
 * @param collen Number of columns 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>rowlen</TT> &lt; 0. Thrown if
 * <TT>collen</TT> &lt; 0. Thrown if any matrix index would be out of
 * bounds.
 */
public void reduce(int dstrow, int dstcol, int[][] src, int srcrow, int srccol, int rowlen, int collen, IntegerOp op) {
    if (rowlen < 0 || collen < 0 || dstrow < 0 || dstrow + rowlen > rows() || dstcol < 0 || dstcol + collen > cols() || srcrow < 0 || srcrow + rowlen > src.length || srccol < 0 || srccol + collen > src[0].length) {
        throw new IndexOutOfBoundsException();
    }
    for (int r = 0; r < rowlen; ++r) {
        AtomicIntegerArray myMatrix_r = myMatrix[dstrow + r];
        int[] src_r = src[srcrow + r];
        for (int c = 0; c < collen; ++c) {
            int dstcol_c = dstcol + c;
            int src_r_c = src_r[srccol + c];
            updateLoop: for (; ; ) {
                int oldvalue = myMatrix_r.get(dstcol_c);
                int newvalue = op.op(oldvalue, src_r_c);
                if (myMatrix_r.compareAndSet(dstcol_c, oldvalue, newvalue)) {
                    break updateLoop;
                }
            }
        }
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 28 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project antlr4 by tunnelvisionlabs.

the class ArrayEdgeMap method putAll.

@Override
@SuppressWarnings("deprecation")
public ArrayEdgeMap<T> putAll(EdgeMap<? extends T> m) {
    if (m.isEmpty()) {
        return this;
    }
    if (m instanceof HashEdgeMap<?>) {
        HashEdgeMap<? extends T> other = (HashEdgeMap<? extends T>) m;
        AtomicIntegerArray keys = other.getKeys();
        T[] values = other.getValues();
        ArrayEdgeMap<T> result = this;
        for (int i = 0; i < values.length; i++) {
            T value = values[i];
            if (value != null) {
                result = result.put(keys.get(i), value);
            }
        }
        return result;
    } else if (m instanceof ArrayEdgeMap<?>) {
        ArrayEdgeMap<? extends T> other = (ArrayEdgeMap<? extends T>) m;
        int minOverlap = Math.max(minIndex, other.minIndex);
        int maxOverlap = Math.min(maxIndex, other.maxIndex);
        ArrayEdgeMap<T> result = this;
        for (int i = minOverlap; i <= maxOverlap; i++) {
            result = result.put(i, m.get(i));
        }
        return result;
    } else if (m instanceof SingletonEdgeMap<?>) {
        SingletonEdgeMap<? extends T> other = (SingletonEdgeMap<? extends T>) m;
        assert !other.isEmpty();
        return put(other.getKey(), other.getValue());
    } else if (m instanceof SparseEdgeMap<?>) {
        SparseEdgeMap<? extends T> other = (SparseEdgeMap<? extends T>) m;
        synchronized (other) {
            int[] keys = other.getKeys();
            List<? extends T> values = other.getValues();
            ArrayEdgeMap<T> result = this;
            for (int i = 0; i < values.size(); i++) {
                result = result.put(keys[i], values.get(i));
            }
            return result;
        }
    } else {
        throw new UnsupportedOperationException(String.format("EdgeMap of type %s is supported yet.", m.getClass().getName()));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) List(java.util.List)

Example 29 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project neo4j by neo4j.

the class IndexStatisticsStoreTest method shouldHandleConcurrentUpdatesWithCheckpointing.

@Test
void shouldHandleConcurrentUpdatesWithCheckpointing() throws Throwable {
    // given
    Race race = new Race();
    AtomicBoolean checkpointDone = new AtomicBoolean();
    int contestantsPerIndex = 5;
    int indexes = 3;
    int delta = 5;
    AtomicIntegerArray expected = new AtomicIntegerArray(indexes);
    race.addContestant(throwing(() -> {
        for (int i = 0; i < 20; i++) {
            Thread.sleep(5);
            store.checkpoint(CursorContext.NULL);
        }
        checkpointDone.set(true);
    }));
    for (int i = 0; i < indexes; i++) {
        int indexId = i;
        store.replaceStats(indexId, new IndexSample(0, 0, 0));
        race.addContestants(contestantsPerIndex, () -> {
            while (!checkpointDone.get()) {
                store.incrementIndexUpdates(indexId, delta);
                expected.addAndGet(indexId, delta);
            }
        });
    }
    // when
    race.go();
    // then
    for (int i = 0; i < indexes; i++) {
        assertEquals(new IndexSample(0, 0, 0, expected.get(i)), store.indexSample(i));
    }
    restartStore();
    for (int i = 0; i < indexes; i++) {
        assertEquals(new IndexSample(0, 0, 0, expected.get(i)), store.indexSample(i));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexSample(org.neo4j.kernel.api.index.IndexSample) Race(org.neo4j.test.Race) Test(org.junit.jupiter.api.Test)

Example 30 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project fastjson by alibaba.

the class AtomicCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    if (parser.lexer.token() == JSONToken.NULL) {
        parser.lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    JSONArray array = new JSONArray();
    parser.parseArray(array);
    if (clazz == AtomicIntegerArray.class) {
        AtomicIntegerArray atomicArray = new AtomicIntegerArray(array.size());
        for (int i = 0; i < array.size(); ++i) {
            atomicArray.set(i, array.getInteger(i));
        }
        return (T) atomicArray;
    }
    AtomicLongArray atomicArray = new AtomicLongArray(array.size());
    for (int i = 0; i < array.size(); ++i) {
        atomicArray.set(i, array.getLong(i));
    }
    return (T) atomicArray;
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) JSONArray(com.alibaba.fastjson.JSONArray) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Aggregations

AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)85 Test (org.junit.Test)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Test (org.junit.jupiter.api.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 List (java.util.List)6 AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 JSONArray (com.alibaba.fastjson.JSONArray)3 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 CacheException (javax.cache.CacheException)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 GridRandom (org.apache.ignite.internal.util.GridRandom)3 CAX (org.apache.ignite.internal.util.typedef.CAX)3