Java Code Examples for java.util.concurrent.atomic.AtomicLong#getAndAdd()

The following examples show how to use java.util.concurrent.atomic.AtomicLong#getAndAdd() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: artio   File: SenderEndPointTest.java    License: Apache License 2.0 6 votes vote down vote up
private AtomicCounter fakeCounter()
{
    final AtomicLong value = new AtomicLong();
    final AtomicCounter atomicCounter = mock(AtomicCounter.class);
    final Answer<Long> get = inv -> value.get();
    final Answer<?> set = inv ->
    {
        value.set(inv.getArgument(0));
        return null;
    };

    final Answer<?> add = (inv) -> value.getAndAdd(inv.getArgument(0));

    when(atomicCounter.get()).then(get);
    when(atomicCounter.getWeak()).then(get);

    doAnswer(set).when(atomicCounter).set(anyLong());
    doAnswer(set).when(atomicCounter).setOrdered(anyLong());
    doAnswer(set).when(atomicCounter).setWeak(anyLong());

    when(atomicCounter.getAndAdd(anyLong())).then(add);
    when(atomicCounter.getAndAddOrdered(anyLong())).then(add);

    return atomicCounter;
}
 
Example 2
Source Project: datawave   File: Counts.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Add to the counter
 *
 * @param key
 * @param value
 */
public void add(K key, long value) {
    updateLock.lock();
    try {
        AtomicLong current = counts.get(key);
        
        if (current == null) {
            current = initCounter(key);
            numRecords.getAndIncrement();
        }
        
        current.getAndAdd(value);
        
    } finally {
        updateLock.unlock();
    }
}
 
Example 3
Source Project: titan1withtp3.1   File: MockIDAuthority.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public IDBlock getIDBlock(final int partition, final int idNamespace, Duration timeout) throws BackendException {
    //Delay artificially
    if (delayAcquisitionMS>0) {
        try {
            Thread.sleep(delayAcquisitionMS);
        } catch (InterruptedException e) {
            throw new TemporaryBackendException(e);
        }
    }
    Preconditions.checkArgument(partition>=0 && partition<=Integer.MAX_VALUE);
    Preconditions.checkArgument(idNamespace>=0 && idNamespace<=Integer.MAX_VALUE);
    Long p = (((long)partition)<<Integer.SIZE) + ((long)idNamespace);
    long size = blockSizer.getBlockSize(idNamespace);
    AtomicLong id = ids.get(p);
    if (id == null) {
        ids.putIfAbsent(p, new AtomicLong(1));
        id = ids.get(p);
        Preconditions.checkNotNull(id);
    }
    long lowerBound = id.getAndAdd(size);
    if (lowerBound >= blockSizeLimit) {
        throw new IDPoolExhaustedException("Reached partition limit: " + blockSizeLimit);
    }
    return new MockIDBlock(lowerBound,Math.min(size,blockSizeLimit-lowerBound));
}
 
Example 4
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 5
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 6
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 7
Source Project: a-foundation   File: ForkJoinPool.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 8
/**
 * Set number of topics
 * @param val Value to set to
 */
public void updateNumTopics(long val) {
  long delta = val - _numTopics.getAndSet(val);
  AtomicLong aggregatedMetric = AGGREGATED_NUM_TOPICS.get(_className);
  if (aggregatedMetric != null) {
    aggregatedMetric.getAndAdd(delta);
  }
}
 
Example 9
Source Project: JDKSourceCode1.8   File: ForkJoinPool.java    License: MIT License 5 votes vote down vote up
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 10
/**
 * Set number of auto paused partitions awaiting destination topic creation
 * @param val Value to set to
 */
public void updateNumAutoPausedPartitionsAwaitingDestTopic(long val) {
  long delta = val - _numAutoPausedPartitionsAwaitingDestTopic.getAndSet(val);
  AtomicLong aggregatedMetric = AGGREGATED_NUM_AUTO_PAUSED_PARTITIONS_WAITING_FOR_DEST_TOPIC.get(_className);
  if (aggregatedMetric != null) {
    aggregatedMetric.getAndAdd(delta);
  }
}
 
Example 11
/**
 *
 * @throws Exception If failed.
 */
@Test
public void testUsedSpaceSize() throws Exception {
    final int DIRS_COUNT = 5;
    final int DIRS_MAX_DEEP = 3;
    final int FILES_COUNT = 10;
    final AtomicLong totalSize = new AtomicLong();

    IgniteBiInClosure<Integer, IgfsPath> createHierarchy = new IgniteBiInClosure<Integer, IgfsPath>() {
        @Override public void apply(Integer level, IgfsPath levelDir) {
            try {
                for (int i = 0; i < FILES_COUNT; ++i) {
                    IgfsPath filePath = new IgfsPath(levelDir, "file" + Integer.toString(i));

                    createFile(igfs, filePath, true, chunk);

                    totalSize.getAndAdd(chunk.length);
                }

                if (level < DIRS_MAX_DEEP) {
                    for (int dir = 0; dir < DIRS_COUNT; dir++) {
                        IgfsPath dirPath = new IgfsPath(levelDir, "dir" + Integer.toString(dir));

                        igfs.mkdirs(dirPath);

                        apply(level + 1, dirPath);
                    }
                }
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };

    createHierarchy.apply(1, new IgfsPath("/dir"));

    assertEquals(totalSize.get(), igfs.metrics().secondarySpaceSize());
}
 
Example 12
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 13
/**
 * Set number of auto paused partitions on in-flight messages
 * @param val Value to set to
 */
public void updateNumAutoPausedPartitionsOnInFlightMessages(long val) {
  long delta = val - _numAutoPausedPartitionsOnInFlightMessages.getAndSet(val);
  AtomicLong aggregatedMetric = AGGREGATED_NUM_AUTO_PAUSED_PARTITIONS_ON_INFLIGHT_MESSAGES.get(_className);
  if (aggregatedMetric != null) {
    aggregatedMetric.getAndAdd(delta);
  }
}
 
Example 14
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 15
Source Project: Java8CN   File: ForkJoinPool.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 16
/**
 * Adds steal count to pool stealCounter if it exists, and resets.
 */
final void transferStealCount(ForkJoinPool p) {
    AtomicLong sc;
    if (p != null && (sc = p.stealCounter) != null) {
        int s = nsteals;
        nsteals = 0;            // if negative, correct for overflow
        sc.getAndAdd((long)(s < 0 ? Integer.MAX_VALUE : s));
    }
}
 
Example 17
public void updateNumPartitions(long val) {
  long delta = val - _numPartitions.getAndSet(val);
  AtomicLong aggregatedMetric = AGGREGATED_NUM_PARTITIONS.get(_className);
  if (aggregatedMetric != null) {
    aggregatedMetric.getAndAdd(delta);
  }
}
 
Example 18
Source Project: PeonyFramwork   File: MonitorService.java    License: Apache License 2.0 4 votes vote down vote up
public void decrMonitorNum(MonitorNumType key,int num){
    AtomicLong atomicLong = monitorNumData.get(key);
    atomicLong.getAndAdd(-num);
}
 
Example 19
@Test
public void test() {
    AtomicLong time = new AtomicLong(0);
    Clock wallClock = Clock.mock(time);
    Reservoir reservoir = new HdrBuilder(wallClock)
            .resetReservoirPeriodically(Duration.ofMillis(1000))
            .withBackgroundExecutor(MockExecutor.INSTANCE)
            .buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot snapshot = reservoir.getSnapshot();
    assertEquals(10, snapshot.getMin());
    assertEquals(20, snapshot.getMax());

    time.getAndAdd(900); // 900
    reservoir.update(30);
    reservoir.update(40);
    snapshot = reservoir.getSnapshot();
    assertEquals(10, snapshot.getMin());
    assertEquals(40, snapshot.getMax());

    time.getAndAdd(99); // 999
    reservoir.update(8);
    reservoir.update(60);
    snapshot = reservoir.getSnapshot();
    assertEquals(8, snapshot.getMin());
    assertEquals(60, snapshot.getMax());

    time.getAndAdd(1); // 1000
    reservoir.update(70);
    reservoir.update(80);
    snapshot = reservoir.getSnapshot();
    assertEquals(70, snapshot.getMin());
    assertEquals(80, snapshot.getMax());

    time.getAndAdd(1001); // 2001
    reservoir.update(90);
    reservoir.update(100);
    snapshot = reservoir.getSnapshot();
    assertEquals(90, snapshot.getMin());
    assertEquals(100, snapshot.getMax());

    time.getAndAdd(1000); // 3001
    snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMin());
    assertEquals(0, snapshot.getMax());

    time.getAndAdd(1); // 3002
    reservoir.update(42);
    snapshot = reservoir.getSnapshot();
    assertEquals(42, snapshot.getMin());
    assertEquals(42, snapshot.getMax());

    time.getAndAdd(2000); // 5002
    snapshot = reservoir.getSnapshot();
    assertEquals(0, snapshot.getMin());
    assertEquals(0, snapshot.getMax());
}
 
Example 20
@Test
public void shouldCacheSnapshot() {
    AtomicLong time = new AtomicLong(System.currentTimeMillis());
    Clock wallClock = Clock.mock(time);
    Reservoir reservoir = new HdrBuilder(wallClock)
            .resetReservoirOnSnapshot()
            .withSnapshotCachingDuration(Duration.ofMillis(1000))
            .buildReservoir();

    reservoir.update(10);
    reservoir.update(20);
    Snapshot firstSnapshot = reservoir.getSnapshot();

    time.getAndAdd(900);
    reservoir.update(30);
    reservoir.update(40);
    Snapshot firstCachedSnapshot = reservoir.getSnapshot();
    assertSame(firstSnapshot, firstCachedSnapshot);
    assertEquals(10, firstCachedSnapshot.getMin());
    assertEquals(20, firstCachedSnapshot.getMax());

    time.getAndAdd(99);
    reservoir.update(50);
    reservoir.update(60);
    Snapshot secondCachedSnapshot = reservoir.getSnapshot();
    assertSame(firstSnapshot, secondCachedSnapshot);
    assertEquals(10, secondCachedSnapshot.getMin());
    assertEquals(20, secondCachedSnapshot.getMax());

    time.getAndAdd(1);
    reservoir.update(70);
    reservoir.update(80);
    Snapshot firstNewSnapshot = reservoir.getSnapshot();
    assertNotSame(firstSnapshot, firstNewSnapshot);
    assertEquals(30, firstNewSnapshot.getMin());
    assertEquals(80, firstNewSnapshot.getMax());

    time.getAndAdd(1001);
    reservoir.update(90);
    reservoir.update(100);
    Snapshot secondNewSnapshot = reservoir.getSnapshot();
    assertNotSame(firstNewSnapshot, secondNewSnapshot);
    assertEquals(90, secondNewSnapshot.getMin());
    assertEquals(100, secondNewSnapshot.getMax());
}