private static void testAccumulate() { LongBinaryOperator op = (x, y) -> 2 * x + y; LongAccumulator accumulator = new LongAccumulator(op, 1L); ExecutorService executor = Executors.newFixedThreadPool(2); IntStream.range(0, 10) .forEach(i -> executor.submit(() -> accumulator.accumulate(i))); ConcurrentUtils.stop(executor); System.out.format("Add: %d\n", accumulator.getThenReset()); } }
/** * Return the current statistics resetting the internal values if reset is true. */ private DTimeMetricStats getStatistics(boolean reset) { if (reset) { // Note these values are not guaranteed to be consistent wrt each other // but should be reasonably consistent (small time between count and total) final long beans = beanCount.sumThenReset(); final long maxVal = max.getThenReset(); final long totalVal = total.sumThenReset(); final long countVal = count.sumThenReset(); final long startTimeVal = startTime.getAndSet(System.currentTimeMillis()); return new DTimeMetricStats(metricType, name, startTimeVal, countVal, totalVal, maxVal, beans); } else { return new DTimeMetricStats(metricType, name, startTime.get(), count.sum(), total.sum(), max.get(), beanCount.sum()); } }
private static void testAccumulate() { LongBinaryOperator op = (x, y) -> 2 * x + y; LongAccumulator accumulator = new LongAccumulator(op, 1L); ExecutorService executor = Executors.newFixedThreadPool(2); IntStream.range(0, 10) .forEach(i -> executor.submit(() -> accumulator.accumulate(i))); ConcurrentUtils.stop(executor); System.out.format("Add: %d\n", accumulator.getThenReset()); } }
private final long num = SimpleReservoir.this.num.sumThenReset(); private final long sum = SimpleReservoir.this.sum.sumThenReset(); private final long max = SimpleReservoir.this.max.getThenReset(); private final long min = SimpleReservoir.this.min.getThenReset();
/** * Return the current statistics reseting the internal values if reset is true. */ public ValueStatistics getStatistics(boolean reset) { if (reset) { // Note these values are not guaranteed to be consistent wrt each other // but should be reasonably consistent (small time between count and total) final long startTimeVal = startTime.getAndSet(System.currentTimeMillis()); final long countVal = count.sumThenReset(); final long totalVal = total.sumThenReset(); final long maxVal = max.getThenReset(); return new DefaultValueStatistics(startTimeVal, countVal, totalVal, maxVal); } else { return new DefaultValueStatistics(startTime.get(), count.sum(), total.sum(), max.get()); } }
/** * Return a Snapshot of the query execution statistics potentially resetting the internal counters. */ Snapshot getSnapshot(boolean reset) { List<MetaQueryPlanOriginCount> origins = getOrigins(reset); // not guaranteed to be consistent due to time gaps between getting each value out of LongAdders but can live with that // relative to the cost of making sure count and totalTime etc are all guaranteed to be consistent if (reset) { return new Snapshot(queryPlan, count.sumThenReset(), totalTime.sumThenReset(), totalBeans.sumThenReset(), maxTime.getThenReset(), startTime.getAndSet(System.currentTimeMillis()), lastQueryTime, origins); } return new Snapshot(queryPlan, count.sum(), totalTime.sum(), totalBeans.sum(), maxTime.get(), startTime.get(), lastQueryTime, origins); }
/** * Return the current statistics resetting the internal values if reset is true. */ private DTimeMetricStats getStatistics(boolean reset) { if (reset) { // Note these values are not guaranteed to be consistent wrt each other // but should be reasonably consistent (small time between count and total) final long beans = beanCount.sumThenReset(); final long maxVal = max.getThenReset(); final long totalVal = total.sumThenReset(); final long countVal = count.sumThenReset(); final long startTimeVal = startTime.getAndSet(System.currentTimeMillis()); return new DTimeMetricStats(metricType, name, startTimeVal, countVal, totalVal, maxVal, beans); } else { return new DTimeMetricStats(metricType, name, startTime.get(), count.sum(), total.sum(), max.get(), beanCount.sum()); } }