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

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

Introduction

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

Prototype

public final int getAndAdd(int 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:Main.java

public static void main(String[] argv) throws Exception {
    AtomicInteger atomicInteger = new AtomicInteger();

    System.out.println(atomicInteger.getAndAdd(10));
    System.out.println(atomicInteger.getAndAdd(10));
}

From source file:csv.sorting.PrepareWeatherData.java

public static void main(String[] args) throws Exception {

    // Path to read the CSV data from:
    final Path csvStationDataFilePath = FileSystems.getDefault()
            .getPath("C:\\Users\\philipp\\Downloads\\csv\\201503station.txt");
    final Path csvLocalWeatherDataUnsortedFilePath = FileSystems.getDefault()
            .getPath("C:\\Users\\philipp\\Downloads\\csv\\201503hourly.txt");
    final Path csvLocalWeatherDataSortedFilePath = FileSystems.getDefault()
            .getPath("C:\\Users\\philipp\\Downloads\\csv\\201503hourly_sorted.txt");

    // A map between the WBAN and Station for faster Lookups:
    final Map<String, Station> stationMap = getStationMap(csvStationDataFilePath);

    // Holds the List of Sorted DateTimes (including ZoneOffset):
    List<Integer> indices = new ArrayList<>();

    // Comparator for sorting the File:
    Comparator<OffsetDateTime> byMeasurementTime = (e1, e2) -> e1.compareTo(e2);

    // Get the sorted indices from the stream of LocalWeatherData Elements:
    try (Stream<CsvMappingResult<csv.model.LocalWeatherData>> stream = getLocalWeatherData(
            csvLocalWeatherDataUnsortedFilePath)) {

        // Holds the current line index, when processing the input Stream:
        AtomicInteger currentIndex = new AtomicInteger(1);

        // We want to get a list of indices, which sorts the CSV file by measurement time:
        indices = stream/*from   ww  w.j  a va  2s .  c o  m*/
                // Skip the CSV Header:
                .skip(1)
                // Start by enumerating ALL mapping results:
                .map(x -> new ImmutablePair<>(currentIndex.getAndAdd(1), x))
                // Then only take those lines, that are actually valid:
                .filter(x -> x.getRight().isValid())
                // Now take the parsed entity from the CsvMappingResult:
                .map(x -> new ImmutablePair<>(x.getLeft(), x.getRight().getResult()))
                // Take only those measurements, that are also available in the list of stations:
                .filter(x -> stationMap.containsKey(x.getRight().getWban()))
                // Get the OffsetDateTime from the LocalWeatherData, which includes the ZoneOffset of the Station:
                .map(x -> {
                    // Get the matching station:
                    csv.model.Station station = stationMap.get(x.getRight().getWban());
                    // Calculate the OffsetDateTime from the given measurement:
                    OffsetDateTime measurementTime = OffsetDateTime.of(x.getRight().getDate(),
                            x.getRight().getTime(), ZoneOffset.ofHours(0));
                    // Build the Immutable pair with the Index again:
                    return new ImmutablePair<>(x.getLeft(), measurementTime);
                })
                // Now sort the Measurements by their Timestamp:
                .sorted((x, y) -> byMeasurementTime.compare(x.getRight(), y.getRight()))
                // Take only the Index:
                .map(x -> x.getLeft())
                // And turn it into a List:
                .collect(Collectors.toList());
    }

    // Now sorts the File by Line Number:
    writeSortedFileByIndices(csvLocalWeatherDataUnsortedFilePath, indices, csvLocalWeatherDataSortedFilePath);
}

From source file:gr.aueb.cs.nlp.wordtagger.classifier.MetaClassifier.java

/**
 * returns the Word with the proper feature vector created fromt he output of input classifiers
 * @param inputWord//from   w  w w. j ava  2  s.  com
 * @return
 */
private Word tagsToFeats(Word inputWord) {
    double[][] feats = new double[1][0];
    AtomicInteger i = new AtomicInteger(0);
    classifiers.forEach(v -> {
        feats[0] = ArrayUtils.addAll(feats[0], model.getCategoryAsOneOfAKDouble(v.classify(inputWord)));
        i.getAndAdd(totalCategories);
    });
    return new Word(inputWord.getValue(), inputWord.getCategory(),
            new FeatureVector(feats[0], model.getCategoryAsOneOfAKDouble(inputWord)));
}

From source file:com.zhaimi.message.kafka.KafkaReceiver.java

private void processStreamsByTopic(String topicKeys, List<KafkaStream<byte[], byte[]>> streamList) {
    // init stream thread pool
    ExecutorService streamPool = Executors.newFixedThreadPool(partitions);
    String[] topics = StringUtils.split(topicKeys, ",");
    if (log.isDebugEnabled())
        log.debug("???? KafkaStreamList,topic count={},topics={}, partitions/topic={}",
                topics.length, topicKeys, partitions);

    //??stream//  www .j a  v a 2 s.c om
    AtomicInteger index = new AtomicInteger(0);
    for (KafkaStream<byte[], byte[]> stream : streamList) {
        Thread streamThread = new Thread() {

            @Override
            public void run() {
                int i = index.getAndAdd(1);
                if (log.isDebugEnabled())
                    log.debug("???KafkaStream -- No.={}, partitions={}", i, partitions + ":" + i);

                ConsumerIterator<byte[], byte[]> consumerIterator = stream.iterator();

                processStreamByConsumer(topicKeys, consumerIterator);
            }
        };
        streamPool.execute(streamThread);
    }
}

From source file:org.apache.flume.channel.kafka.TestKafkaChannel.java

private List<Event> pullEvents(final KafkaChannel channel, ExecutorCompletionService<Void> submitterSvc,
        final int total, final boolean testRollbacks, final boolean retryAfterRollback) {
    final List<Event> eventsPulled = Collections.synchronizedList(new ArrayList<Event>(50));
    final CyclicBarrier barrier = new CyclicBarrier(5);
    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicInteger rolledBackCount = new AtomicInteger(0);
    final AtomicBoolean startedGettingEvents = new AtomicBoolean(false);
    final AtomicBoolean rolledBack = new AtomicBoolean(false);
    for (int k = 0; k < 5; k++) {
        final int index = k;
        submitterSvc.submit(new Callable<Void>() {
            @Override//from w ww .  j a  v a 2  s.  c  om
            public Void call() throws Exception {
                Transaction tx = null;
                final List<Event> eventsLocal = Lists.newLinkedList();
                int takenByThisThread = 0;
                channel.registerThread();
                Thread.sleep(1000);
                barrier.await();
                while (counter.get() < (total - rolledBackCount.get())) {
                    if (tx == null) {
                        tx = channel.getTransaction();
                        tx.begin();
                    }
                    try {
                        Event e = channel.take();
                        if (e != null) {
                            startedGettingEvents.set(true);
                            eventsLocal.add(e);
                        } else {
                            if (testRollbacks && index == 4 && (!rolledBack.get())
                                    && startedGettingEvents.get()) {
                                tx.rollback();
                                tx.close();
                                tx = null;
                                rolledBack.set(true);
                                final int eventsLocalSize = eventsLocal.size();
                                eventsLocal.clear();
                                if (!retryAfterRollback) {
                                    rolledBackCount.set(eventsLocalSize);
                                    return null;
                                }
                            } else {
                                tx.commit();
                                tx.close();
                                tx = null;
                                eventsPulled.addAll(eventsLocal);
                                counter.getAndAdd(eventsLocal.size());
                                eventsLocal.clear();
                            }
                        }
                    } catch (Exception ex) {
                        eventsLocal.clear();
                        if (tx != null) {
                            tx.rollback();
                            tx.close();
                        }
                        tx = null;
                        ex.printStackTrace();
                    }
                }
                // Close txn.
                return null;
            }
        });
    }
    return eventsPulled;
}

From source file:org.apache.tinkerpop.gremlin.structure.TransactionTest.java

@Test
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = FEATURE_DOUBLE_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES)
@FeatureRequirement(featureClass = Graph.Features.EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
@FeatureRequirement(featureClass = Graph.Features.EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_INTEGER_VALUES)
public void shouldExecuteWithCompetingThreads() {
    int totalThreads = 250;
    final AtomicInteger vertices = new AtomicInteger(0);
    final AtomicInteger edges = new AtomicInteger(0);
    final AtomicInteger completedThreads = new AtomicInteger(0);
    for (int i = 0; i < totalThreads; i++) {
        new Thread() {
            @Override//w w w.  j a  va2 s . com
            public void run() {
                final Random random = new Random();
                if (random.nextBoolean()) {
                    final Vertex a = graph.addVertex();
                    final Vertex b = graph.addVertex();
                    final Edge e = a.addEdge("friend", b);

                    vertices.getAndAdd(2);
                    a.property(VertexProperty.Cardinality.single, "test", this.getId());
                    b.property(VertexProperty.Cardinality.single, "blah", random.nextDouble());
                    e.property("bloop", random.nextInt());
                    edges.getAndAdd(1);
                    graph.tx().commit();
                } else {
                    final Vertex a = graph.addVertex();
                    final Vertex b = graph.addVertex();
                    final Edge e = a.addEdge("friend", b);

                    a.property(VertexProperty.Cardinality.single, "test", this.getId());
                    b.property(VertexProperty.Cardinality.single, "blah", random.nextDouble());
                    e.property("bloop", random.nextInt());

                    if (random.nextBoolean()) {
                        graph.tx().commit();
                        vertices.getAndAdd(2);
                        edges.getAndAdd(1);
                    } else {
                        graph.tx().rollback();
                    }
                }
                completedThreads.getAndAdd(1);
            }
        }.start();
    }

    while (completedThreads.get() < totalThreads) {
    }

    assertEquals(completedThreads.get(), 250);
    assertVertexEdgeCounts(vertices.get(), edges.get());
}

From source file:org.codice.ddf.commands.catalog.ExportCommand.java

private int logPartition(String e, AtomicInteger counter) {
    return counter.getAndAdd(e.length() + SECURITY_AUDIT_DELIMITER.length()) / LOG4J_MAX_BUF_SIZE;
}

From source file:org.dllearner.algorithms.qtl.experiments.BenchmarkDescriptionGenerator.java

private DescriptiveStatistics determineDefaultCBDSizes(Query query, List<String> resources) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    NumberFormat df = DecimalFormat.getPercentInstance();
    AtomicInteger idx = new AtomicInteger(1);

    CBDStructureTree cbdStructure = getDefaultCBDStructureTree();
    System.out.println(cbdStructure.toStringVerbose());

    ProgressBar progressBar = new ProgressBar();

    resources.forEach(r -> {/*from   www  .j  a v  a2 s . c  om*/
        long cnt = -1;
        if (useConstruct) {
            Model cbd = null;
            try {
                //               cbd = cbdGen.getConciseBoundedDescription(r, cbdStructure);
                //               cnt = cbd.size();
                //               System.out.println(r + ":" + cnt);
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e.getCause());
            }

        } else {
            ParameterizedSparqlString template = SPARQLUtils.CBD_TEMPLATE_DEPTH3.copy();
            template.setIri("uri", r);
            try (QueryExecution qe = qef.createQueryExecution(template.toString())) {
                ResultSet rs = qe.execSelect();
                cnt = rs.next().getLiteral("cnt").getInt();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e.getCause());
            }
        }
        stats.addValue(cnt);
        progressBar.update(idx.getAndAdd(1), resources.size());

    });

    return stats;
}

From source file:org.dllearner.algorithms.qtl.experiments.BenchmarkDescriptionGenerator.java

private DescriptiveStatistics determineOptimalCBDSizes(Query query, List<String> resources) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    NumberFormat df = DecimalFormat.getPercentInstance();
    AtomicInteger idx = new AtomicInteger(1);

    CBDStructureTree cbdStructure = QueryUtils.getOptimalCBDStructure(query);
    System.out.println(cbdStructure.toStringVerbose());

    ProgressBar progressBar = new ProgressBar();

    resources.forEach(r -> {//from   w ww  . j a  va 2  s .co m
        long cnt = -1;
        if (useConstruct) {
            Model cbd = null;
            try {
                //               cbd = cbdGen.getConciseBoundedDescription(r, cbdStructure);
                //               cnt = cbd.size();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e.getCause());
            }

        } else {
            ParameterizedSparqlString template = SPARQLUtils.CBD_TEMPLATE_DEPTH3.copy();
            template.setIri("uri", r);
            try (QueryExecution qe = qef.createQueryExecution(template.toString())) {
                ResultSet rs = qe.execSelect();
                cnt = rs.next().getLiteral("cnt").getInt();
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e.getCause());
            }
        }
        stats.addValue(cnt);
        progressBar.update(idx.getAndAdd(1), resources.size());

    });

    return stats;
}

From source file:org.easyrec.plugin.slopeone.impl.SlopeOneServiceImpl.java

public void nonPersonalizedRecommendations(final SlopeOneIntegerConfiguration config, final SlopeOneStats stats,
        final Date execution, final Set<TenantItem> changedItemIds, final ExecutionControl control) {
    final long start = System.currentTimeMillis();

    final int MAX_ITEMASSOCS = 50000;
    // atomic to support usage in changedItemIds.forEach(AnonymousClass)
    final AtomicInteger itemAssocCount = new AtomicInteger(0);

    final int TOTAL_STEPS = changedItemIds.size();
    // atomic to support usage in changedItemIds.forEach(AnonymousClass)
    final AtomicInteger currentStep = new AtomicInteger(0);

    Integer maxRecsPerItem = config.getMaxRecsPerItem();
    final List<ItemAssocVO<Integer, Integer>> itemAssocs = new ArrayList<ItemAssocVO<Integer, Integer>>(
            Math.min(changedItemIds.size() * (maxRecsPerItem != null ? maxRecsPerItem : 10), MAX_ITEMASSOCS));

    for (TenantItem changedItem : changedItemIds) {
        if (control != null)
            control.updateProgress(String.format("Calculating non-personalized recommendations %d/%d",
                    currentStep.getAndIncrement(), TOTAL_STEPS));

        List<Deviation> deviations = deviationDAO.getDeviationsOrdered(config.getTenant(),
                changedItem.getItemTypeId(), changedItem.getItemId(), config.getMinRatedCount(),
                config.getMaxRecsPerItem());

        for (Deviation deviation : deviations) {
            ItemAssocVO<Integer, Integer> assoc = new ItemAssocVO<Integer, Integer>(config.getTenant(),
                    deviation.getItem1(), config.getAssocType(), deviation.getDeviation(), deviation.getItem2(),
                    config.getSourceType(), config.getNonPersonalizedSourceInfo(), config.getViewType(),
                    Boolean.TRUE, execution);

            itemAssocs.add(assoc);/*w w w .  j  a  va  2  s.  c  o  m*/
        }

        if (itemAssocs.size() >= MAX_ITEMASSOCS) {
            itemAssocCount.getAndAdd((itemAssocs.size()));
            itemAssocDAO.insertOrUpdateItemAssocs(itemAssocs);
            itemAssocs.clear();
        }
    }

    if (itemAssocs.size() > 0) {
        itemAssocCount.getAndAdd((itemAssocs.size()));
        itemAssocDAO.insertOrUpdateItemAssocs(itemAssocs);
        itemAssocs.clear();
    }

    itemAssocDAO.removeItemAssocByTenant(config.getTenant(), config.getAssocType(), config.getSourceType(),
            execution);

    stats.setNumberOfRulesCreated(itemAssocCount.get());
    stats.setNonPersonalizedDuration(System.currentTimeMillis() - start);
}