Search in sources :

Example 56 with AtomicIntegerArray

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

the class ForkedProcessorStepTest method shouldKeepForkedOrderIntactWhenChangingProcessorCount.

@Test
void shouldKeepForkedOrderIntactWhenChangingProcessorCount() throws Exception {
    int length = 100;
    AtomicIntegerArray reference = new AtomicIntegerArray(length);
    // GIVEN
    StageControl control = new SimpleStageControl();
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    ForkedProcessorStep<int[]> step = new ForkedProcessorStep<>(control, "Processor", config(availableProcessors)) {

        @Override
        protected void forkedProcess(int id, int processors, int[] batch) throws InterruptedException {
            int ticket = batch[0];
            Thread.sleep(ThreadLocalRandom.current().nextInt(10));
            for (int i = 1; i < batch.length; i++) {
                if (batch[i] % processors == id) {
                    boolean compareAndSet = reference.compareAndSet(batch[i], ticket, ticket + 1);
                    assertTrue(compareAndSet, "I am " + id + ". Was expecting " + ticket + " for " + batch[i] + " but was " + reference.get(batch[i]));
                }
            }
        }
    };
    DeadEndStep downstream = new DeadEndStep(control);
    step.setDownstream(downstream);
    // WHEN
    step.start(0);
    downstream.start(0);
    ThreadLocalRandom random = ThreadLocalRandom.current();
    for (int ticket = 0; ticket < 200; ticket++) {
        // a particular batch.
        if (random.nextFloat() < 0.1) {
            step.processors(random.nextInt(-2, 4));
        }
        int[] batch = new int[length];
        batch[0] = ticket;
        for (int j = 1; j < batch.length; j++) {
            batch[j] = j - 1;
        }
        step.receive(ticket, batch);
    }
    step.endOfUpstream();
    step.awaitCompleted();
    step.close();
    downstream.close();
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.jupiter.api.Test)

Example 57 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project neo4j-apoc-procedures by neo4j-contrib.

the class PageRankArrayStorageParallelCypher method compute.

public void compute(int iterations, int[] sourceDegreeData, int[] sourceChunkStartingIndex, int[] relationshipTarget, int[] relationshipWeight) {
    previousPageRanks = new int[nodeCount];
    pageRanksAtomic = new AtomicIntegerArray(nodeCount);
    stats.iterations = iterations;
    long before = System.currentTimeMillis();
    for (int iteration = 0; iteration < iterations; iteration++) {
        long beforeIteration = System.currentTimeMillis();
        startIteration(sourceChunkStartingIndex, sourceDegreeData, relationshipWeight);
        iterateParallel(iteration, sourceDegreeData, sourceChunkStartingIndex, relationshipTarget, relationshipWeight);
        long afterIteration = System.currentTimeMillis();
        log.info("Time for iteration " + iteration + "  " + (afterIteration - beforeIteration) + " millis");
    }
    long after = System.currentTimeMillis();
    stats.computeMillis = (after - before);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 58 with AtomicIntegerArray

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

the class AtomicCodec method write.

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
    SerializeWriter out = serializer.out;
    if (object instanceof AtomicInteger) {
        AtomicInteger val = (AtomicInteger) object;
        out.writeInt(val.get());
        return;
    }
    if (object instanceof AtomicLong) {
        AtomicLong val = (AtomicLong) object;
        out.writeLong(val.get());
        return;
    }
    if (object instanceof AtomicBoolean) {
        AtomicBoolean val = (AtomicBoolean) object;
        out.append(val.get() ? "true" : "false");
        return;
    }
    if (object == null) {
        out.writeNull(SerializerFeature.WriteNullListAsEmpty);
        return;
    }
    if (object instanceof AtomicIntegerArray) {
        AtomicIntegerArray array = (AtomicIntegerArray) object;
        int len = array.length();
        out.write('[');
        for (int i = 0; i < len; ++i) {
            int val = array.get(i);
            if (i != 0) {
                out.write(',');
            }
            out.writeInt(val);
        }
        out.write(']');
        return;
    }
    AtomicLongArray array = (AtomicLongArray) object;
    int len = array.length();
    out.write('[');
    for (int i = 0; i < len; ++i) {
        long val = array.get(i);
        if (i != 0) {
            out.write(',');
        }
        out.writeLong(val);
    }
    out.write(']');
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 59 with AtomicIntegerArray

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

the class Bug_7 method test_AtomicIntegerArray.

public void test_AtomicIntegerArray() throws Exception {
    AtomicIntegerArray array = new AtomicIntegerArray(3);
    array.set(0, 1);
    array.set(1, 2);
    array.set(2, 3);
    String text = JSON.toJSONString(array);
    Assert.assertEquals("[1,2,3]", text);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 60 with AtomicIntegerArray

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

the class PostponedSnapshotTestBase method setup.

@Before
public void setup() {
    instance = createHazelcastInstance();
    latches = new AtomicIntegerArray(2);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Before(org.junit.Before)

Aggregations

AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)74 Test (org.junit.Test)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 List (java.util.List)5 AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)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 AtomicIntegerArrayAssertBaseTest (org.assertj.core.api.AtomicIntegerArrayAssertBaseTest)3 JSONArray (com.alibaba.fastjson.JSONArray)2