Code example for AtomicDouble

0
 
        averages = new TimeSeries((long) (keepDuration * 1e3), (long) (measureInterval * 1e3));
        tick = (long) (measureInterval * 1e9);
        alpha = 1 - Math.exp(-tick / (averageInterval * 1e9));
        tickOffset = new AtomicLong(System.nanoTime() / tick);
        total = new AtomicDouble();
    } 
 
    // Updates the average if it's past time to measure. 
    private void tick(long time) {
        long oldOffset = tickOffset.get();
        long newOffset = time / tick;
        long offsetDiff = newOffset - oldOffset;
 
        if (offsetDiff > 0L &&
                tickOffset.compareAndSet(oldOffset, newOffset)) {
            double oldAverage = total.getAndSet(0.0) / tick;
 
            for (long i = 0L, size = offsetDiff - 1L; i < size; ++ i) {
                currentAverage += alpha * (0L - currentAverage);