Example usage for java.util.concurrent.atomic AtomicInteger incrementAndGet

List of usage examples for java.util.concurrent.atomic AtomicInteger incrementAndGet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger incrementAndGet.

Prototype

public final int incrementAndGet() 

Source Link

Document

Atomically increments the current value, with memory effects as specified by VarHandle#getAndAdd .

Usage

From source file:nl.rivm.cib.episim.model.disease.infection.MSEIRSTest.java

private <T> Observable<Entry<T, Stream<BigDecimal>>> averages(
        final Supplier<Observable<Entry<Double, long[]>>> sir, final Function<Double, T> bins, final int n) {
    return Observable.create(sub -> {
        final NavigableMap<T, long[]> sums = java.util.Collections
                .synchronizedNavigableMap(new TreeMap<T, long[]>());
        final long t0 = System.currentTimeMillis();
        final AtomicInteger iteration = new AtomicInteger();
        final AtomicLong sysTime = new AtomicLong(t0);
        Observable.range(0, n).flatMap(i -> Observable.just(i).subscribeOn(Schedulers.computation()).map(ii -> {
            final int iii = iteration.incrementAndGet();
            final long t = System.currentTimeMillis();
            sysTime.updateAndGet(t1 -> {
                if (t - t1 > 10000) {
                    LOG.trace("Progress {}% at ~{}/s, iteration {} of {}",
                            DecimalUtil.floor(DecimalUtil.divide(iii * 100, n)),
                            DecimalUtil.round(DecimalUtil.divide(iii * 1000, t - t0)), iii, n);
                    return t;
                }/*from  w ww . j  a  v  a2 s  . c o  m*/
                return t1;
            });
            return sir.get()
                    // group by bin size
                    .groupBy(yt -> bins.apply(yt.getKey()))
                    // take highest floating point t in this bin
                    .flatMap(gr -> gr.reduce((yt1, yt2) -> yt1.getKey().compareTo(yt2.getKey()) > 0 ? yt1 : yt2)
                            .toObservable().map(yt -> Collections.entry(gr.getKey(), yt.getValue())))
                    // add to current sums
                    .collect(() -> sums,
                            (sum, yt) -> sum.compute(yt.getKey(),
                                    (k, v) -> v == null ? yt.getValue()
                                            : IntStream.range(0, v.length)
                                                    .mapToLong(iv -> v[iv] + yt.getValue()[iv]).toArray()))
                    .blockingGet();
        })).blockingSubscribe();

        sums.forEach((k, v) -> sub
                .onNext(Collections.entry(k, Arrays.stream(v).mapToObj(y -> DecimalUtil.divide(y, n)))));
        final long dt = System.currentTimeMillis() - t0;
        LOG.trace("Completed {} iterations in {}s = {}/s", n,
                DecimalUtil.toScale(DecimalUtil.divide(dt, 1000), 1),
                DecimalUtil.round(DecimalUtil.divide(n * 1000, dt)));
    });
}

From source file:ninja.benchmark.NinjaBenchmark.java

public void execute(final int threads, final int requests, final BenchmarkCall call)
        throws IOException, InterruptedException {
    final OkHttpClient client = new OkHttpClient.Builder()
            .connectionPool(new ConnectionPool(threads, 60000L, TimeUnit.MILLISECONDS)).build();

    final AtomicInteger requested = new AtomicInteger();

    final Request request = call.request;

    // warmup by doing small # of requests first
    for (int i = 0; i < 20; i++) {
        Response response = client.newCall(request).execute();
        response.body().close();//w w  w  . ja  v  a  2 s .c o m
    }

    final CountDownLatch startSignal = new CountDownLatch(1);
    final CountDownLatch doneSignal = new CountDownLatch(threads);
    ExecutorService threadPool = Executors.newFixedThreadPool(threads);

    for (int i = 0; i < threads; i++) {
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    startSignal.await();
                    while (requested.incrementAndGet() < requests) {
                        Response response = client.newCall(request).execute();
                        response.body().close();
                    }
                    doneSignal.countDown();
                } catch (InterruptedException | IOException e) {
                    e.printStackTrace(System.err);
                }
            }
        });
    }

    // real
    Stopwatch stopwatch = Stopwatch.createStarted();
    startSignal.countDown();
    doneSignal.await();
    stopwatch.stop();
    call.setElapsed(stopwatch.elapsed(TimeUnit.MILLISECONDS));

    threadPool.shutdown();
}

From source file:org.apache.hadoop.hbase.client.TestAsyncTable.java

@Test
public void testCheckAndPut() throws InterruptedException, ExecutionException {
    AsyncTableBase table = getTable.get();
    AtomicInteger successCount = new AtomicInteger(0);
    AtomicInteger successIndex = new AtomicInteger(-1);
    int count = 10;
    CountDownLatch latch = new CountDownLatch(count);
    IntStream.range(0, count)//  ww  w  . j a  v  a2 s. c  o m
            .forEach(
                    i -> table
                            .checkAndPut(row, FAMILY, QUALIFIER, null,
                                    new Put(row).addColumn(FAMILY, QUALIFIER, concat(VALUE, i)))
                            .thenAccept(x -> {
                                if (x) {
                                    successCount.incrementAndGet();
                                    successIndex.set(i);
                                }
                                latch.countDown();
                            }));
    latch.await();
    assertEquals(1, successCount.get());
    String actual = Bytes.toString(table.get(new Get(row)).get().getValue(FAMILY, QUALIFIER));
    assertTrue(actual.endsWith(Integer.toString(successIndex.get())));
}

From source file:org.apache.hadoop.hbase.master.procedure.TestMasterProcedureQueue.java

/**
 * Verify that "write" operations for a single table are serialized,
 * but different tables can be executed in parallel.
 *///from  www.ja va 2s  .  c  o  m
@Test(timeout = 90000)
public void testConcurrentWriteOps() throws Exception {
    final TestTableProcSet procSet = new TestTableProcSet(queue);

    final int NUM_ITEMS = 10;
    final int NUM_TABLES = 4;
    final AtomicInteger opsCount = new AtomicInteger(0);
    for (int i = 0; i < NUM_TABLES; ++i) {
        TableName tableName = TableName.valueOf(String.format("testtb-%04d", i));
        for (int j = 1; j < NUM_ITEMS; ++j) {
            procSet.addBack(new TestTableProcedure(i * 100 + j, tableName,
                    TableProcedureInterface.TableOperationType.EDIT));
            opsCount.incrementAndGet();
        }
    }
    assertEquals(opsCount.get(), queue.size());

    final Thread[] threads = new Thread[NUM_TABLES * 2];
    final HashSet<TableName> concurrentTables = new HashSet<TableName>();
    final ArrayList<String> failures = new ArrayList<String>();
    final AtomicInteger concurrentCount = new AtomicInteger(0);
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread() {
            @Override
            public void run() {
                while (opsCount.get() > 0) {
                    try {
                        TableProcedureInterface proc = procSet.acquire();
                        if (proc == null) {
                            queue.signalAll();
                            if (opsCount.get() > 0) {
                                continue;
                            }
                            break;
                        }
                        synchronized (concurrentTables) {
                            assertTrue("unexpected concurrency on " + proc.getTableName(),
                                    concurrentTables.add(proc.getTableName()));
                        }
                        assertTrue(opsCount.decrementAndGet() >= 0);
                        try {
                            long procId = ((Procedure) proc).getProcId();
                            TableName tableId = proc.getTableName();
                            int concurrent = concurrentCount.incrementAndGet();
                            assertTrue("inc-concurrent=" + concurrent + " 1 <= concurrent <= " + NUM_TABLES,
                                    concurrent >= 1 && concurrent <= NUM_TABLES);
                            LOG.debug("[S] tableId=" + tableId + " procId=" + procId + " concurrent="
                                    + concurrent);
                            Thread.sleep(2000);
                            concurrent = concurrentCount.decrementAndGet();
                            LOG.debug("[E] tableId=" + tableId + " procId=" + procId + " concurrent="
                                    + concurrent);
                            assertTrue("dec-concurrent=" + concurrent, concurrent < NUM_TABLES);
                        } finally {
                            synchronized (concurrentTables) {
                                assertTrue(concurrentTables.remove(proc.getTableName()));
                            }
                            procSet.release(proc);
                        }
                    } catch (Throwable e) {
                        LOG.error("Failed " + e.getMessage(), e);
                        synchronized (failures) {
                            failures.add(e.getMessage());
                        }
                    } finally {
                        queue.signalAll();
                    }
                }
            }
        };
        threads[i].start();
    }
    for (int i = 0; i < threads.length; ++i) {
        threads[i].join();
    }
    assertTrue(failures.toString(), failures.isEmpty());
    assertEquals(0, opsCount.get());
    assertEquals(0, queue.size());

    for (int i = 1; i <= NUM_TABLES; ++i) {
        TableName table = TableName.valueOf(String.format("testtb-%04d", i));
        assertTrue("queue should be deleted, table=" + table, queue.markTableAsDeleted(table));
    }
}

From source file:com.adobe.acs.commons.workflow.process.impl.ReplicateWithOptionsWorkflowProcess.java

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
        throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final long start = System.currentTimeMillis();

    try {/*from   w  ww. java  2s  .c om*/
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final String originalPayload = (String) workItem.getWorkflowData().getPayload();
        final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, originalPayload);
        final ProcessArgs processArgs = new ProcessArgs(metaDataMap);

        final AtomicInteger count = new AtomicInteger(0);

        // Anonymous inner class to facilitate counting of processed payloads
        final ResourceRunnable replicatorRunnable = new ResourceRunnable() {
            @Override
            public void run(final Resource resource) throws Exception {
                if (processArgs.isThrottle()) {
                    throttledTaskRunner.waitForLowCpuAndLowMemory();
                }
                replicator.replicate(resource.getResourceResolver().adaptTo(Session.class),
                        processArgs.getReplicationActionType(), resource.getPath(),
                        processArgs.getReplicationOptions(resource));
                count.incrementAndGet();
            }
        };

        final ContentVisitor visitor = new ContentVisitor(replicatorRunnable);

        for (final String payload : payloads) {
            final Resource resource = resourceResolver.getResource(payload);

            if (processArgs.isTraverseTree()) {
                // Traverse the tree
                visitor.accept(resource);
            } else {
                // Only execute on the provided payload
                replicatorRunnable.run(resource);
            }
        }

        log.info("Replicate with Options processed [ {} ] total payloads in {} ms", count.get(),
                System.currentTimeMillis() - start);
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}

From source file:dk.statsbiblioteket.util.xml.XMLStepperTest.java

public void testMultipleInner() throws XMLStreamException {
    final String XML = "<foo><bar><zoo>z1</zoo><zoo>z2</zoo></bar></foo>";
    for (final Boolean step : new boolean[] { Boolean.FALSE, Boolean.TRUE }) {
        XMLStreamReader xml = xmlFactory.createXMLStreamReader(new StringReader(XML));
        XMLStepper.findTagStart(xml, "zoo");
        final AtomicInteger zooCount = new AtomicInteger(0);
        XMLStepper.iterateTags(xml, new XMLStepper.Callback() {
            @Override//from   ww  w  .ja  va 2s  .co  m
            public boolean elementStart(XMLStreamReader xml, List<String> tags, String current)
                    throws XMLStreamException {
                if ("zoo".equals(current)) {
                    zooCount.incrementAndGet();
                }
                if (step) {
                    xml.next();
                    return true;
                }
                return false;
            }
        });
        assertEquals(
                "After iteration with step==" + step
                        + ", the stepper should have encountered 'zoo' the right number of times",
                2, zooCount.get());
        assertEquals(
                "After iteration with step==" + step
                        + ", the reader should be positioned at the correct end tag",
                "bar", xml.getLocalName());
    }

}

From source file:org.apache.hadoop.yarn.client.api.async.impl.TestAMRMClientAsync.java

@SuppressWarnings("unchecked")
@Test(timeout = 10000)/* w w w.  jav  a  2 s  .c om*/
public void testAMRMClientAsync() throws Exception {
    Configuration conf = new Configuration();
    final AtomicBoolean heartbeatBlock = new AtomicBoolean(true);
    List<ContainerStatus> completed1 = Arrays
            .asList(ContainerStatus.newInstance(newContainerId(0, 0, 0, 0), ContainerState.COMPLETE, "", 0));
    List<Container> containers = Arrays.asList(Container.newInstance(null, null, null, null, null, null));
    final AllocateResponse response1 = createAllocateResponse(new ArrayList<ContainerStatus>(), containers,
            null);
    final AllocateResponse response2 = createAllocateResponse(completed1, new ArrayList<Container>(), null);
    final AllocateResponse response3 = createAllocateResponse(new ArrayList<ContainerStatus>(),
            new ArrayList<Container>(), containers, containers, null);
    final AllocateResponse emptyResponse = createAllocateResponse(new ArrayList<ContainerStatus>(),
            new ArrayList<Container>(), null);

    TestCallbackHandler callbackHandler = new TestCallbackHandler();
    final AMRMClient<ContainerRequest> client = mock(AMRMClientImpl.class);
    final AtomicInteger secondHeartbeatSync = new AtomicInteger(0);
    when(client.allocate(anyFloat())).thenReturn(response1).thenAnswer(new Answer<AllocateResponse>() {
        @Override
        public AllocateResponse answer(InvocationOnMock invocation) throws Throwable {
            secondHeartbeatSync.incrementAndGet();
            while (heartbeatBlock.get()) {
                synchronized (heartbeatBlock) {
                    heartbeatBlock.wait();
                }
            }
            secondHeartbeatSync.incrementAndGet();
            return response2;
        }
    }).thenReturn(response3).thenReturn(emptyResponse);
    when(client.registerApplicationMaster(anyString(), anyInt(), anyString())).thenReturn(null);
    when(client.getAvailableResources()).thenAnswer(new Answer<Resource>() {
        @Override
        public Resource answer(InvocationOnMock invocation) throws Throwable {
            // take client lock to simulate behavior of real impl
            synchronized (client) {
                Thread.sleep(10);
            }
            return null;
        }
    });

    AMRMClientAsync<ContainerRequest> asyncClient = AMRMClientAsync.createAMRMClientAsync(client, 20,
            callbackHandler);
    asyncClient.init(conf);
    asyncClient.start();
    asyncClient.registerApplicationMaster("localhost", 1234, null);

    // while the CallbackHandler will still only be processing the first response,
    // heartbeater thread should still be sending heartbeats.
    // To test this, wait for the second heartbeat to be received. 
    while (secondHeartbeatSync.get() < 1) {
        Thread.sleep(10);
    }

    // heartbeat will be blocked. make sure we can call client methods at this
    // time. Checks that heartbeat is not holding onto client lock
    assert (secondHeartbeatSync.get() < 2);
    asyncClient.getAvailableResources();
    // method returned. now unblock heartbeat
    assert (secondHeartbeatSync.get() < 2);
    synchronized (heartbeatBlock) {
        heartbeatBlock.set(false);
        heartbeatBlock.notifyAll();
    }

    // allocated containers should come before completed containers
    Assert.assertEquals(null, callbackHandler.takeCompletedContainers());

    // wait for the allocated containers from the first heartbeat's response
    while (callbackHandler.takeAllocatedContainers() == null) {
        Assert.assertEquals(null, callbackHandler.takeCompletedContainers());
        Thread.sleep(10);
    }

    // wait for the completed containers from the second heartbeat's response
    while (callbackHandler.takeCompletedContainers() == null) {
        Thread.sleep(10);
    }

    // wait for the changed containers from the thrid heartbeat's response
    while (callbackHandler.takeChangedContainers() == null) {
        Thread.sleep(10);
    }

    asyncClient.stop();

    Assert.assertEquals(null, callbackHandler.takeAllocatedContainers());
    Assert.assertEquals(null, callbackHandler.takeCompletedContainers());
    Assert.assertEquals(null, callbackHandler.takeChangedContainers());
}

From source file:com.fizzed.stork.deploy.Archive.java

public Path unpack(Path unpackDir) throws IOException {
    Files.createDirectories(unpackDir);

    log.info("Unpacking {} to {}", file, unpackDir);

    // we need to know the top-level dir(s) created by unpack
    final Set<Path> firstLevelPaths = new LinkedHashSet<>();
    final AtomicInteger count = new AtomicInteger();

    try (ArchiveInputStream ais = newArchiveInputStream(file)) {
        ArchiveEntry entry;//from  w w  w  .  ja  v a 2  s.  c  o m
        while ((entry = ais.getNextEntry()) != null) {
            try {
                Path entryFile = Paths.get(entry.getName());
                Path resolvedFile = unpackDir.resolve(entryFile);

                firstLevelPaths.add(entryFile.getName(0));

                log.debug("{}", resolvedFile);

                if (entry.isDirectory()) {
                    Files.createDirectories(resolvedFile);
                } else {
                    unpackEntry(ais, resolvedFile);
                    count.incrementAndGet();
                }
            } catch (IOException | IllegalStateException | IllegalArgumentException e) {
                log.error("", e);
                throw new RuntimeException(e);
            }
        }
    }

    if (firstLevelPaths.size() != 1) {
        throw new IOException("Only archives with a single top-level directory are supported!");
    }

    Path assemblyDir = unpackDir.resolve(firstLevelPaths.iterator().next());

    log.info("Unpacked {} files to {}", count, assemblyDir);

    return assemblyDir;
}

From source file:com.smoketurner.pipeline.application.core.MessageProcessor.java

/**
 * Stream an {@link S3Object} object and process each line with the
 * processor./*  w w w.  j  ava2  s .  c  om*/
 * 
 * @param object
 *            S3Object to download and process
 * @return number of events processed
 * @throws IOException
 *             if unable to stream the object
 */
private int streamObject(@Nonnull final S3Object object) throws IOException {

    final AtomicInteger eventCount = new AtomicInteger(0);
    try (S3ObjectInputStream input = object.getObjectContent()) {

        final BufferedReader reader;
        if (AmazonS3Downloader.isGZipped(object)) {
            reader = new BufferedReader(
                    new InputStreamReader(new StreamingGZIPInputStream(input), StandardCharsets.UTF_8));
        } else {
            reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        }

        // failed will be true if we did not successfully broadcast all
        // of the events because of no consumers
        final boolean failed = reader.lines().peek(event -> eventCount.incrementAndGet())
                .anyMatch(broadcaster::test);

        if (failed) {
            // abort the current S3 download
            input.abort();
            LOGGER.error("Partial events broadcast ({} sent) from key: {}/{}", eventCount.get(),
                    object.getBucketName(), object.getKey());
            throw new IOException("aborting download");
        }
    }
    return eventCount.get();
}

From source file:cc.gospy.example.google.GoogleScholarSpider.java

public Collection<String> getResultLinks(final String keyword, final int pageFrom, final int pageTo) {
    if (pageFrom < 1)
        throw new IllegalArgumentException(pageFrom + "<" + 1);
    if (pageFrom >= pageTo)
        throw new IllegalArgumentException(pageFrom + ">=" + pageTo);

    final AtomicInteger currentPage = new AtomicInteger(pageFrom);
    final AtomicBoolean returned = new AtomicBoolean(false);
    final Collection<String> links = new LinkedHashSet<>();
    Gospy googleScholarSpider = Gospy.custom()
            .setScheduler(/*from  w  ww.  j a  v  a 2  s  .  co  m*/
                    Schedulers.VerifiableScheduler.custom().setExitCallback(() -> returned.set(true)).build())
            .addFetcher(Fetchers.HttpFetcher.custom()
                    .before(request -> request.setConfig(
                            RequestConfig.custom().setProxy(new HttpHost("127.0.0.1", 8118)).build()))
                    .build())
            .addProcessor(Processors.XPathProcessor.custom().extract(
                    "//*[@id='gs_ccl_results']/div/div/h3/a/@href", (task, resultList, returnedData) -> {
                        links.addAll(resultList);
                        currentPage.incrementAndGet();
                        if (pageFrom <= currentPage.get() && currentPage.get() < pageTo) {
                            return Arrays.asList(
                                    new Task(String.format("https://scholar.google.com/scholar?start=%d&q=%s",
                                            (currentPage.get() - 1) * 10, keyword)));
                        } else {
                            return Arrays.asList();
                        }
                    }).build())
            .build()
            .addTask(String.format("https://scholar.google.com/scholar?start=%d&q=%s", pageFrom, keyword));
    googleScholarSpider.start();
    while (!returned.get())
        ; // block until spider returned
    googleScholarSpider.stop();
    return links;
}