Example usage for java.util.concurrent.atomic AtomicLong addAndGet

List of usage examples for java.util.concurrent.atomic AtomicLong addAndGet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicLong addAndGet.

Prototype

public final long addAndGet(long delta) 

Source Link

Document

Atomically adds the given value to the current value, with memory effects as specified by VarHandle#getAndAdd .

Usage

From source file:com.cloudera.oryx.app.serving.als.model.LoadTestALSModelFactory.java

public static ALSServingModel buildTestModel() {

    log.info("Building load test model...");

    System.gc();/*from  w w  w  . ja va 2s . c o m*/
    long startMemory = JVMUtils.getUsedMemory();

    ALSServingModel model = new ALSServingModel(FEATURES, true, LSH_SAMPLE_RATE, new TestALSRescorerProvider());
    AtomicLong totalEntries = new AtomicLong();

    int numCores = Runtime.getRuntime().availableProcessors();
    log.info("Adding {} users", USERS);
    AtomicInteger userCount = new AtomicInteger();
    ExecUtils.doInParallel(numCores, i -> {
        RandomGenerator random = RandomManager.getRandom(((long) i << 32) ^ System.nanoTime());
        PoissonDistribution itemPerUserDist = new PoissonDistribution(random, AVG_ITEMS_PER_USER,
                PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
        for (int user = userCount.getAndIncrement(); user < USERS; user = userCount.getAndIncrement()) {
            String userID = "U" + user;
            model.setUserVector(userID, VectorMath.randomVectorF(FEATURES, random));
            int itemsPerUser = itemPerUserDist.sample();
            totalEntries.addAndGet(itemsPerUser);
            Collection<String> knownIDs = new ArrayList<>(itemsPerUser);
            for (int item = 0; item < itemsPerUser; item++) {
                knownIDs.add("I" + random.nextInt(ITEMS));
            }
            model.addKnownItems(userID, knownIDs);
        }
    });

    log.info("Adding {} items", ITEMS);
    AtomicInteger itemCount = new AtomicInteger();
    ExecUtils.doInParallel(numCores, i -> {
        RandomGenerator random = RandomManager.getRandom(((long) i << 32) ^ System.nanoTime());
        for (int item = itemCount.getAndIncrement(); item < ITEMS; item = itemCount.getAndIncrement()) {
            model.setItemVector("I" + item, VectorMath.randomVectorF(FEATURES, random));
        }
    });

    System.gc();
    long endMemory = JVMUtils.getUsedMemory();

    log.info("Built model over {} users, {} items, {} features, {} entries, using {}MB", USERS, ITEMS, FEATURES,
            totalEntries, (endMemory - startMemory) / 1_000_000);
    log.info("Model: {}", model);
    return model;
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestBatchIbr.java

static void runIbrTest(final long ibrInterval) throws Exception {
    final ExecutorService executor = createExecutor();
    final Random ran = new Random();

    final Configuration conf = newConf(ibrInterval);
    final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATANODES).build();
    final DistributedFileSystem dfs = cluster.getFileSystem();

    try {/*from w  w  w.j  a v  a  2 s.  co m*/
        final String dirPathString = "/dir";
        final Path dir = new Path(dirPathString);
        dfs.mkdirs(dir);

        // start testing
        final long testStartTime = Time.monotonicNow();
        final ExecutorCompletionService<Path> createService = new ExecutorCompletionService<>(executor);
        final AtomicLong createFileTime = new AtomicLong();
        final AtomicInteger numBlockCreated = new AtomicInteger();

        // create files
        for (int i = 0; i < NUM_FILES; i++) {
            createService.submit(new Callable<Path>() {
                @Override
                public Path call() throws Exception {
                    final long start = Time.monotonicNow();
                    try {
                        final long seed = ran.nextLong();
                        final int numBlocks = ran.nextInt(MAX_BLOCK_NUM) + 1;
                        numBlockCreated.addAndGet(numBlocks);
                        return createFile(dir, numBlocks, seed, dfs);
                    } finally {
                        createFileTime.addAndGet(Time.monotonicNow() - start);
                    }
                }
            });
        }

        // verify files
        final ExecutorCompletionService<Boolean> verifyService = new ExecutorCompletionService<>(executor);
        final AtomicLong verifyFileTime = new AtomicLong();
        for (int i = 0; i < NUM_FILES; i++) {
            final Path file = createService.take().get();
            verifyService.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    final long start = Time.monotonicNow();
                    try {
                        return verifyFile(file, dfs);
                    } finally {
                        verifyFileTime.addAndGet(Time.monotonicNow() - start);
                    }
                }
            });
        }
        for (int i = 0; i < NUM_FILES; i++) {
            Assert.assertTrue(verifyService.take().get());
        }
        final long testEndTime = Time.monotonicNow();

        LOG.info("ibrInterval=" + ibrInterval + " ("
                + toConfString(DFS_BLOCKREPORT_INCREMENTAL_INTERVAL_MSEC_KEY, conf) + "), numBlockCreated="
                + numBlockCreated);
        LOG.info("duration=" + toSecondString(testEndTime - testStartTime) + ", createFileTime="
                + toSecondString(createFileTime.get()) + ", verifyFileTime="
                + toSecondString(verifyFileTime.get()));
        LOG.info("NUM_FILES=" + NUM_FILES + ", MAX_BLOCK_NUM=" + MAX_BLOCK_NUM + ", BLOCK_SIZE=" + BLOCK_SIZE
                + ", NUM_THREADS=" + NUM_THREADS + ", NUM_DATANODES=" + NUM_DATANODES);
        logIbrCounts(cluster.getDataNodes());
    } finally {
        executor.shutdown();
        cluster.shutdown();
    }
}

From source file:com.amaze.filemanager.utils.files.FileUtils.java

/**
 * Helper method to get size of an otg folder
 */// w w w .  j a  v a  2 s  . c  o  m
public static long otgFolderSize(String path, final Context context) {
    final AtomicLong totalBytes = new AtomicLong(0);
    OTGUtil.getDocumentFiles(path, context, file -> totalBytes.addAndGet(getBaseFileSize(file, context)));
    return totalBytes.longValue();
}

From source file:com.netflix.spinnaker.kork.metrics.SpectatorMetricWriter.java

@Override
public void increment(Delta<?> delta) {
    if (delta.getName().startsWith("meter.")) {
        registry.counter(delta.getName()).increment(delta.getValue().longValue());
    } else {/*from w  w  w . j  av a2  s  .c om*/
        final Id id = registry.createId(delta.getName());
        final AtomicLong gauge = getCounterStorage(id);

        gauge.addAndGet(delta.getValue().longValue());
        registry.gauge(delta.getName(), gauge);
    }
}

From source file:org.mycore.common.xml.MCRXMLFunctions.java

/**
 * Method returns the amount of space consumed by the files contained in the
 * derivate container. The returned string is already formatted meaning it
 * has already the optimal measurement unit attached (e.g. 142 MB, ).
 *
 * @param derivateId/*from w  w  w.  ja  v  a  2  s.  co  m*/
 *            the derivate id for which the size should be returned
 * @return the size as formatted string
 */
public static String getSize(String derivateId) throws IOException {
    MCRPath rootPath = MCRPath.getPath(derivateId, "/");
    final AtomicLong size = new AtomicLong();
    Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            size.addAndGet(attrs.size());
            return super.visitFile(file, attrs);
        }

    });
    return MCRUtils.getSizeFormatted(size.get());
}

From source file:ubicrypt.core.Utils.java

private static void read(final AtomicLong pos, final ByteBuffer buffer, final AsynchronousFileChannel channel,
        final Subscriber<? super byte[]> subscriber) {
    channel.read(buffer, pos.get(), pos, new CompletionHandler<Integer, AtomicLong>() {
        @Override/*from   ww  w . ja v  a 2 s . c  om*/
        public void completed(final Integer result, final AtomicLong attachment) {
            if (result == -1) {
                subscriber.onCompleted();
                return;
            }
            subscriber.onNext(buffer.array());
            if (result < 1 << 16) {
                subscriber.onCompleted();
                return;
            }
            pos.addAndGet(result);
            read(pos, ByteBuffer.allocate(1 << 16), channel, subscriber);
        }

        @Override
        public void failed(final Throwable exc, final AtomicLong attachment) {
            subscriber.onError(exc);
        }
    });
}

From source file:org.archive.crawler.admin.StatisticsTracker.java

/**
 * Increment a counter for a key in a given cache by an arbitrary amount.
 * Used for various aggregate data. The increment amount can be negative.
 *
 *
 * @param cache/*from   w ww  .j  a va2 s  . c  o  m*/
 *            The ObjectIdentityCache
 * @param key
 *            The key for the counter to be incremented, if it does not exist
 *            it will be added (set to equal to <code>increment</code>).
 *            If null it will increment the counter "unknown".
 * @param increment
 *            The amount to increment counter related to the <code>key</code>.
 */
protected static void incrementCacheCount(ObjectIdentityCache<String, AtomicLong> cache, String key,
        long increment) {
    if (key == null) {
        key = "unknown";
    }
    AtomicLong lw = cache.getOrUse(key, ATOMIC_ZERO_SUPPLIER);
    lw.addAndGet(increment);
}

From source file:org.archive.crawler.reporting.StatisticsTracker.java

/**
 * Increment a counter for a key in a given HashMap by an arbitrary amount.
 * Used for various aggregate data. The increment amount can be negative.
 *
 *
 * @param map//from ww  w  .j a  va  2 s . co  m
 *            The HashMap
 * @param key
 *            The key for the counter to be incremented, if it does not exist
 *            it will be added (set to equal to <code>increment</code>).
 *            If null it will increment the counter "unknown".
 * @param increment
 *            The amount to increment counter related to the <code>key</code>.
 */
protected static void incrementMapCount(ConcurrentMap<String, AtomicLong> map, String key, long increment) {
    if (key == null) {
        key = "unknown";
    }
    AtomicLong lw = (AtomicLong) map.get(key);
    if (lw == null) {
        lw = new AtomicLong(0);
        AtomicLong prevVal = map.putIfAbsent(key, lw);
        if (prevVal != null) {
            lw = prevVal;
        }
    }
    lw.addAndGet(increment);
}

From source file:it.polimi.diceH2020.SPACE4CloudWS.core.DataProcessor.java

private long calculateMetric(@NonNull List<SolutionPerJob> spjList,
        BiConsumer<SolutionPerJob, Double> resultSaver, Consumer<SolutionPerJob> ifEmpty) {
    //to support also parallel stream.
    AtomicLong executionTime = new AtomicLong();

    spjList.forEach(spj -> {/* w  w  w  .java  2s  . c  om*/
        Pair<Optional<Double>, Long> result = simulateClass(spj);
        executionTime.addAndGet(result.getRight());
        Optional<Double> optionalValue = result.getLeft();
        if (optionalValue.isPresent())
            resultSaver.accept(spj, optionalValue.get());
        else
            ifEmpty.accept(spj);
    });

    return executionTime.get();
}

From source file:ubicrypt.core.util.OnSubscribeInputStreamTest.java

@Test
public void testBig() throws Exception {
    final AtomicLong count = new AtomicLong();
    final CountDownLatch cd = new CountDownLatch(1);
    Observable/*w w w .  ja va2 s. c  o m*/
            .create(new OnSubscribeInputStream(
                    new ByteArrayInputStream(StringUtils.repeat('a', 2 << 16).getBytes()), 1 << 16))
            .doOnCompleted(cd::countDown).subscribe(next -> count.addAndGet(next.length));
    assertThat(cd.await(2, TimeUnit.SECONDS)).isTrue();
    assertThat(count.get()).isEqualTo(2 << 16);
}