Search in sources :

Example 46 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project cxf by apache.

the class NioBookStore method uploadBookStream.

@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public void uploadBookStream(@Suspended AsyncResponse response) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final byte[] buffer = new byte[4096];
    final LongAdder adder = new LongAdder();
    new NioReadEntity(// read handler
    in -> {
        final int n = in.read(buffer);
        if (n > 0) {
            adder.add(n);
            out.write(buffer, 0, n);
        }
    }, // completion handler
    () -> {
        closeOutputStream(out);
        response.resume("Book Store uploaded: " + adder.longValue() + " bytes");
    });
}
Also used : NioReadEntity(org.apache.cxf.jaxrs.nio.NioReadEntity) LongAdder(java.util.concurrent.atomic.LongAdder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 47 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project drools by kiegroup.

the class CompositeAgendaTest method testCreateHaltDisposeAgenda.

@Test
@Ignore
public void testCreateHaltDisposeAgenda() {
    final String drl = " import org.drools.compiler.integrationtests.facts.*;\n" + " declare A @role( event ) end\n" + " global java.util.concurrent.atomic.LongAdder firings;\n" + " rule R0 when\n" + "     A( value > 0,$Aid: id )\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R1 when\n" + "     A(value > 1)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R2 when\n" + "     A(value > 2)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R3 when\n" + "     A(value > 3)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R4 when\n" + "     A(value > 4)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R5 when\n" + "     A(value > 5)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R6 when\n" + "     A(value > 6)\n" + " then\n" + "     firings.add(1);\n" + " end\n" + " rule R7 when\n" + "     A(value > 7)\n" + " then\n" + "     firings.add(1);\n" + " end";
    final KieBaseConfiguration kieBaseConfiguration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieBaseConfiguration.setOption(MultithreadEvaluationOption.YES);
    kieBaseConfiguration.setOption(EventProcessingOption.STREAM);
    final KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(kieBaseConfiguration);
    final KieSession kieSession = kieBase.newKieSession();
    final LongAdder firingCounter = new LongAdder();
    kieSession.setGlobal("firings", firingCounter);
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit(() -> kieSession.fireUntilHalt());
    final EventInsertThread eventInsertThread = new EventInsertThread(kieSession);
    executor.submit(eventInsertThread);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    kieSession.halt();
    eventInsertThread.setActive(false);
    kieSession.dispose();
    executor.shutdown();
    try {
        if (!executor.awaitTermination(10, TimeUnit.MILLISECONDS)) {
            executor.shutdownNow();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) LongAdder(java.util.concurrent.atomic.LongAdder) KieBase(org.kie.api.KieBase) ExecutorService(java.util.concurrent.ExecutorService) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 48 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project mapdb by jankotek.

the class ConcurrentHashMap8Test method testMappedForEachEntrySequentially.

/**
     * Mapped forEachEntrySequentially traverses the given
     * transformations of all entries
     */
public void testMappedForEachEntrySequentially() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEachEntry(Long.MAX_VALUE, (Map.Entry<Long, Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder)

Example 49 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project mapdb by jankotek.

the class ConcurrentHashMap8Test method testMappedForEachInParallel.

/**
     * Mapped forEachInParallel traverses the given
     * transformations of all mappings
     */
public void testMappedForEachInParallel() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap<Long, Long> m = longMap();
    m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), (Long x) -> adder.add(x.longValue()));
    assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder)

Example 50 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project mapdb by jankotek.

the class ConcurrentHashMap8Test method testKeySetSpliterator.

/**
     * KeySetView.spliterator returns spliterator over the elements in this set
     */
public void testKeySetSpliterator() {
    LongAdder adder = new LongAdder();
    ConcurrentHashMap map = map5();
    Set set = map.keySet();
    Spliterator<Integer> sp = set.spliterator();
    checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
    assertEquals(sp.estimateSize(), map.size());
    Spliterator<Integer> sp2 = sp.trySplit();
    sp.forEachRemaining((Integer x) -> adder.add(x.longValue()));
    long v = adder.sumThenReset();
    sp2.forEachRemaining((Integer x) -> adder.add(x.longValue()));
    long v2 = adder.sum();
    assertEquals(v + v2, 15);
}
Also used : Set(java.util.Set) LongAdder(java.util.concurrent.atomic.LongAdder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

LongAdder (java.util.concurrent.atomic.LongAdder)92 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Random (java.util.Random)13 ExecutorService (java.util.concurrent.ExecutorService)10 SplittableRandom (java.util.SplittableRandom)9 HashMap (java.util.HashMap)7 Map (java.util.Map)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Theory (org.junit.experimental.theories.Theory)6 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Ignite (org.apache.ignite.Ignite)4 IgniteUuid (org.apache.ignite.lang.IgniteUuid)4 Metric (org.springframework.boot.actuate.metrics.Metric)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Callable (java.util.concurrent.Callable)3 CountDownLatch (java.util.concurrent.CountDownLatch)3