本文整理匯總了Java中java.util.concurrent.atomic.AtomicLong.addAndGet方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicLong.addAndGet方法的具體用法?Java AtomicLong.addAndGet怎麽用?Java AtomicLong.addAndGet使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.atomic.AtomicLong
的用法示例。
在下文中一共展示了AtomicLong.addAndGet方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: gatherAllCopyResults
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
private Metrics gatherAllCopyResults() {
AtomicLong bytesReplicated = new AtomicLong(0);
registerRunningMetrics(bytesReplicated);
for (Copy copyJob : copyJobs) {
try {
copyJob.waitForCompletion();
long alreadyReplicated = bytesReplicated.addAndGet(copyJob.getProgress().getTotalBytesToTransfer());
if (totalBytesToReplicate > 0) {
LOG.info("Replicating...': {}% complete",
String.format("%.0f", (alreadyReplicated / (double) totalBytesToReplicate) * 100.0));
}
} catch (InterruptedException e) {
throw new CircusTrainException(e);
}
}
ImmutableMap<String, Long> metrics = ImmutableMap.of(S3S3CopierMetrics.Metrics.TOTAL_BYTES_TO_REPLICATE.name(),
totalBytesToReplicate);
return new S3S3CopierMetrics(metrics, bytesReplicated.get());
}
示例2: doPut
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
private void doPut(String tableName, final List<com.alicloud.tablestore.adaptor.struct.OPut> puts) throws IOException {
ArrayList<com.alicloud.tablestore.adaptor.struct.OPut> tableWriteBuffer = getTableWriteBuffer(tableName);
AtomicLong currentBufferSize = getTableCurrentBufferSize(tableName);
boolean autoFlush = getAutoFlush(tableName);
long writeBufferSize = getWriteBufferSize(tableName);
List<com.alicloud.tablestore.adaptor.struct.OPut> flushPuts = null;
synchronized (tableWriteBuffer) {
for (com.alicloud.tablestore.adaptor.struct.OPut put : puts) {
tableWriteBuffer.add(put);
currentBufferSize.addAndGet(put.getWritableSize());
}
if (autoFlush || currentBufferSize.get() > writeBufferSize) {
flushPuts = new ArrayList<com.alicloud.tablestore.adaptor.struct.OPut>(tableWriteBuffer);
tableWriteBuffer.clear();
currentBufferSize.set(0);
}
}
if (flushPuts != null && !flushPuts.isEmpty()) {
doCommits(tableName, flushPuts);
}
}
示例3: doCommits
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
private void doCommits(String tableName, final List<com.alicloud.tablestore.adaptor.struct.OPut> puts) throws IOException {
boolean flushSuccessfully = false;
try {
otsProxy.putMultiple(tableName, puts);
flushSuccessfully = true;
} finally {
if (!flushSuccessfully && !getClearBufferOnFail(tableName)) {
ArrayList<com.alicloud.tablestore.adaptor.struct.OPut> tableWriteBuffer = getTableWriteBuffer(tableName);
synchronized (tableWriteBuffer) {
AtomicLong currentBufferSize = getTableCurrentBufferSize(tableName);
for (com.alicloud.tablestore.adaptor.struct.OPut put : puts) {
tableWriteBuffer.add(put);
currentBufferSize.addAndGet(put.getWritableSize());
}
}
}
}
}
示例4: updateCounter
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
/**
* Increment the counter
* @param counterName counter name
* @param updateValue increment value
*/
public void updateCounter(String counterName, int updateValue) {
AtomicLong counter = metrics.get(counterName);
if(counter == null) {
counter = metrics.putIfAbsent(counterName, new AtomicLong(0));
if(counter == null) {
counter = metrics.get(counterName);
}
}
counter.addAndGet(updateValue);
}
示例5: generateTableData
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
/**
* Load the tuples for the given table name
* @param tableName
*/
protected void generateTableData(String tableName) {
LOG.debug("Starting data generator for '" + tableName + "'");
final AbstractTableGenerator generator = this.generators.get(tableName);
assert(generator != null);
long tableSize = generator.getTableSize();
long batchSize = generator.getBatchSize();
final AtomicLong table_ctr = this.table_sizes.get(tableName);
VoltTable table = generator.getVoltTable();
LOG.info("Loading " + tableSize + " tuples for table '" + tableName + "'");
while (generator.hasMore()) {
generator.addRow();
if (table.getRowCount() >= batchSize) {
if (table_ctr.get() % 100000 == 0) LOG.info(String.format(tableName + ": loading %d rows (id %d of %d)", table.getRowCount(), generator.getCount(), tableSize));
loadVoltTable(tableName, table);
table_ctr.addAndGet(table.getRowCount());
table.clearRowData();
}
} // WHILE
if (table.getRowCount() > 0) {
LOG.info(tableName + ": loading final " + table.getRowCount() + " rows.");
loadVoltTable(tableName, table);
this.table_sizes.get(tableName).addAndGet(table.getRowCount());
table.clearRowData();
}
LOG.info(tableName + ": Inserted " + this.table_sizes.get(tableName) + " tuples");
}
示例6: testConcurrentDeleteByQueriesOnSameDocs
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
public void testConcurrentDeleteByQueriesOnSameDocs() throws Throwable {
final long docs = randomIntBetween(50, 100);
List<IndexRequestBuilder> builders = new ArrayList<>();
for (int i = 0; i < docs; i++) {
builders.add(client().prepareIndex("test", "doc", String.valueOf(i)).setSource("foo", "bar"));
}
indexRandom(true, true, true, builders);
final Thread[] threads = new Thread[scaledRandomIntBetween(2, 9)];
final CountDownLatch start = new CountDownLatch(1);
final MatchQueryBuilder query = matchQuery("foo", "bar");
final AtomicLong deleted = new AtomicLong(0);
for (int t = 0; t < threads.length; t++) {
Runnable r = () -> {
try {
start.await();
BulkByScrollResponse response = deleteByQuery().source("test").filter(query).refresh(true).get();
// Some deletions might fail due to version conflict, but
// what matters here is the total of successful deletions
deleted.addAndGet(response.getDeleted());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
threads[t] = new Thread(r);
threads[t].start();
}
start.countDown();
for (Thread thread : threads) {
thread.join();
}
assertHitCount(client().prepareSearch("test").setSize(0).get(), 0L);
assertThat(deleted.get(), equalTo(docs));
}
示例7: testGetAgeInMillis
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
public void testGetAgeInMillis() throws Exception {
AtomicLong time = new AtomicLong();
PrioritizedRunnable runnable = new PrioritizedRunnable(Priority.NORMAL, time::get) {
@Override
public void run() {
}
};
assertEquals(0, runnable.getAgeInMillis());
int milliseconds = randomIntBetween(1, 256);
time.addAndGet(TimeUnit.NANOSECONDS.convert(milliseconds, TimeUnit.MILLISECONDS));
assertEquals(milliseconds, runnable.getAgeInMillis());
}
示例8: merge
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
public void merge(CountCollector other){
for(Map.Entry<String,AtomicLong> otherEntry : MapTool.nullSafe(other.getCountByKey()).entrySet()){
AtomicLong existingValue = countByKey.get(otherEntry.getKey());
if(existingValue != null){
existingValue.addAndGet(otherEntry.getValue().longValue());
}else{
countByKey.put(otherEntry.getKey(), new AtomicLong(otherEntry.getValue().longValue()));
}
}
}
示例9: pollQueue
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
void pollQueue() {
int emitted = 0;
AtomicLong localRequested = this.requested;
AtomicLong localCounter = this.counter;
do {
localCounter.set(1);
long produced = 0;
long r = localRequested.get();
while (!this.child.isUnsubscribed()) {
if (this.finished) {
Throwable error = this.error;
if (error != null) {
this.queue.clear();
this.child.onError(error);
return;
} else if (this.queue.isEmpty()) {
this.child.onCompleted();
return;
}
}
if (r > 0) {
Object o = this.queue.poll();
if (o != null) {
this.child.onNext(this.on.getValue(o));
r--;
emitted++;
produced++;
}
}
if (produced > 0 && localRequested.get() != Long.MAX_VALUE) {
localRequested.addAndGet(-produced);
}
}
return;
} while (localCounter.decrementAndGet() > 0);
if (emitted > 0) {
request((long) emitted);
}
}
示例10: addToCounter
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
/**
* @param counterName
* @param delta
*/
public void addToCounter(String counterName, long delta) {
AtomicLong c = this.counters.get(counterName);
if (c != null) {
c.addAndGet(delta);
}
}
示例11: postCompleteDrain
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
/**
* Drains the queue based on the outstanding requests in post-completed mode (only!).
*
* @param n the current request amount
* @param actual the target Subscriber to send events to
* @param queue the queue to drain if in the post-complete state
* @param state holds the request amount and the post-completed flag
* @param isCancelled a supplier that returns true if the drain has been cancelled
* @return true if the queue was completely drained or the drain process was cancelled
*/
static <T> boolean postCompleteDrain(long n,
Subscriber<? super T> actual,
Queue<T> queue,
AtomicLong state,
BooleanSupplier isCancelled) {
// TODO enable fast-path
// if (n == -1 || n == Long.MAX_VALUE) {
// for (;;) {
// if (isCancelled.getAsBoolean()) {
// break;
// }
//
// T v = queue.poll();
//
// if (v == null) {
// actual.onComplete();
// break;
// }
//
// actual.onNext(v);
// }
//
// return true;
// }
long e = n & COMPLETED_MASK;
for (; ; ) {
while (e != n) {
if (isCancelled(isCancelled)) {
return true;
}
T t = queue.poll();
if (t == null) {
actual.onComplete();
return true;
}
actual.onNext(t);
e++;
}
if (isCancelled(isCancelled)) {
return true;
}
if (queue.isEmpty()) {
actual.onComplete();
return true;
}
n = state.get();
if (n == e) {
n = state.addAndGet(-(e & REQUESTED_MASK));
if ((n & REQUESTED_MASK) == 0L) {
return false;
}
e = n & COMPLETED_MASK;
}
}
}
示例12: emitLoop
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
void emitLoop() {
Subscriber<? super R> child = this.child;
Queue<Object> queue = this.queue;
NotificationLite<R> nl = NotificationLite.instance();
AtomicLong requested = this.requested;
long r = requested.get();
while (true) {
boolean max = r == Long.MAX_VALUE;
if (!checkTerminated(this.done, queue.isEmpty(), child)) {
long e = 0;
while (r != 0) {
boolean d = this.done;
Object o = queue.poll();
boolean empty = o == null;
if (!checkTerminated(d, empty, child)) {
if (empty) {
break;
}
R v = nl.getValue(o);
try {
child.onNext(v);
r--;
e--;
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
child.onError(OnErrorThrowable.addValueAsLastCause(ex, v));
return;
}
}
return;
}
if (!(e == 0 || max)) {
r = requested.addAndGet(e);
}
synchronized (this) {
if (this.missed) {
this.missed = false;
} else {
this.emitting = false;
return;
}
}
}
return;
}
}
示例13: incr
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
@Override
public void incr(String group, String name, long count) {
AtomicLong counter = getToAdd(group, name, AtomicLong.class, counterLock, counters);
counter.addAndGet(count);
}
示例14: BucketAllocator
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
/**
* Rebuild the allocator's data structures from a persisted map.
* @param availableSpace capacity of cache
* @param map A map stores the block key and BucketEntry(block's meta data
* like offset, length)
* @param realCacheSize cached data size statistics for bucket cache
* @throws BucketAllocatorException
*/
BucketAllocator(long availableSpace, int[] bucketSizes, Map<BlockCacheKey, BucketEntry> map,
AtomicLong realCacheSize) throws BucketAllocatorException {
this(availableSpace, bucketSizes);
// each bucket has an offset, sizeindex. probably the buckets are too big
// in our default state. so what we do is reconfigure them according to what
// we've found. we can only reconfigure each bucket once; if more than once,
// we know there's a bug, so we just log the info, throw, and start again...
boolean[] reconfigured = new boolean[buckets.length];
for (Map.Entry<BlockCacheKey, BucketEntry> entry : map.entrySet()) {
long foundOffset = entry.getValue().offset();
int foundLen = entry.getValue().getLength();
int bucketSizeIndex = -1;
for (int i = 0; i < bucketSizes.length; ++i) {
if (foundLen <= bucketSizes[i]) {
bucketSizeIndex = i;
break;
}
}
if (bucketSizeIndex == -1) {
throw new BucketAllocatorException(
"Can't match bucket size for the block with size " + foundLen);
}
int bucketNo = (int) (foundOffset / bucketCapacity);
if (bucketNo < 0 || bucketNo >= buckets.length)
throw new BucketAllocatorException("Can't find bucket " + bucketNo
+ ", total buckets=" + buckets.length
+ "; did you shrink the cache?");
Bucket b = buckets[bucketNo];
if (reconfigured[bucketNo]) {
if (b.sizeIndex() != bucketSizeIndex)
throw new BucketAllocatorException(
"Inconsistent allocation in bucket map;");
} else {
if (!b.isCompletelyFree())
throw new BucketAllocatorException("Reconfiguring bucket "
+ bucketNo + " but it's already allocated; corrupt data");
// Need to remove the bucket from whichever list it's currently in at
// the moment...
BucketSizeInfo bsi = bucketSizeInfos[bucketSizeIndex];
BucketSizeInfo oldbsi = bucketSizeInfos[b.sizeIndex()];
oldbsi.removeBucket(b);
bsi.instantiateBucket(b);
reconfigured[bucketNo] = true;
}
realCacheSize.addAndGet(foundLen);
buckets[bucketNo].addAllocation(foundOffset);
usedSize += buckets[bucketNo].getItemAllocationSize();
bucketSizeInfos[bucketSizeIndex].blockAllocated(b);
}
}
示例15: testInsertUpdateDelete
import java.util.concurrent.atomic.AtomicLong; //導入方法依賴的package包/類
@Test
public void testInsertUpdateDelete() throws Exception {
final int NTHREAD = 2;
procStore.stop(false);
fs.delete(logDir, true);
org.apache.hadoop.conf.Configuration conf =
new org.apache.hadoop.conf.Configuration(htu.getConfiguration());
conf.setBoolean("hbase.procedure.store.wal.use.hsync", false);
conf.setInt("hbase.procedure.store.wal.periodic.roll.msec", 10000);
conf.setInt("hbase.procedure.store.wal.roll.threshold", 128 * 1024);
fs.mkdirs(logDir);
procStore = ProcedureTestingUtility.createWalStore(conf, fs, logDir);
procStore.start(NTHREAD);
procStore.recoverLease();
LoadCounter loader = new LoadCounter();
procStore.load(loader);
assertEquals(0, loader.getMaxProcId());
assertEquals(0, loader.getLoadedCount());
assertEquals(0, loader.getCorruptedCount());
final long LAST_PROC_ID = 9999;
final Thread[] thread = new Thread[NTHREAD];
final AtomicLong procCounter = new AtomicLong((long)Math.round(Math.random() * 100));
for (int i = 0; i < thread.length; ++i) {
thread[i] = new Thread() {
@Override
public void run() {
Random rand = new Random();
TestProcedure proc;
do {
proc = new TestProcedure(procCounter.addAndGet(1));
// Insert
procStore.insert(proc, null);
// Update
for (int i = 0, nupdates = rand.nextInt(10); i <= nupdates; ++i) {
try { Thread.sleep(0, rand.nextInt(15)); } catch (InterruptedException e) {}
procStore.update(proc);
}
// Delete
procStore.delete(proc.getProcId());
} while (proc.getProcId() < LAST_PROC_ID);
}
};
thread[i].start();
}
for (int i = 0; i < thread.length; ++i) {
thread[i].join();
}
procStore.getStoreTracker().dump();
assertTrue(procCounter.get() >= LAST_PROC_ID);
assertTrue(procStore.getStoreTracker().isEmpty());
assertEquals(1, procStore.getActiveLogs().size());
}