private static void executeAtomicLong() { System.out.println("### AtomicLong Execution Started... ###"); IAtomicLong atomicLong = context.getBean("atomicLong", IAtomicLong.class); atomicLong.set(100L); System.out.println("AtomicLong is set to 100."); System.out.println("AtomicLong: " + atomicLong.get() + "\n"); }
/** * Increments and then return current cache version. Atomic. */ public long incrementAndGet() { return version.incrementAndGet(); }
public static void main(String[] args) { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IAtomicLong counter = hz.getAtomicLong("counter"); for (int i = 0; i < 1000 * 1000; i++) { if (i % 500000 == 0) { System.out.println("At: " + i); } counter.incrementAndGet(); } System.out.println(format("Count is %s", counter.get())); Hazelcast.shutdownAll(); } }
public static void main(String[] args) { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IAtomicLong atomicLong = hz.getAtomicLong("counter"); atomicLong.set(1); long result = atomicLong.apply(new Add2Function()); System.out.println("apply.result:" + result); System.out.println("apply.value:" + atomicLong.get()); atomicLong.set(1); atomicLong.alter(new Add2Function()); System.out.println("alter.value:" + atomicLong.get()); atomicLong.set(1); result = atomicLong.alterAndGet(new Add2Function()); System.out.println("alterAndGet.result:" + result); System.out.println("alterAndGet.value:" + atomicLong.get()); atomicLong.set(1); result = atomicLong.getAndAlter(new Add2Function()); System.out.println("getAndAlter.result:" + result); System.out.println("getAndAlter.value:" + atomicLong.get()); System.exit(0); for (; ; ) { long oldValue = atomicLong.get(); long newValue = oldValue + 2; if (atomicLong.compareAndSet(oldValue, newValue)) { break; } } }
/** * Returns current cache version */ public long get() { return version.get(); }
@Override public void setCapacity(int size) { maxQueueSize.set(size); }
public static void main(String[] args) { // Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1 HazelcastInstance hz = HazelcastClient.newHazelcastClient(); // Get an Atomic Counter, we'll call it "counter" IAtomicLong counter = hz.getAtomicLong("counter"); // Add and Get the "counter" counter.addAndGet(3); // value is now 3 // Display the "counter" value System.out.println("counter: " + counter.get()); // Shutdown this Hazelcast Client hz.shutdown(); } }
@Warmup(global = true) public void warmup() throws Exception { for (int k = 0; k < lockCount; k++) { long key = lockCounter.getAndIncrement(); targetInstance.getLock(getLockId(key)); IAtomicLong account = targetInstance.getAtomicLong(getAccountId(key)); account.set(initialAmount); totalMoney.addAndGet(initialAmount); } }
@BeforeRun public void beforeRun(ThreadState state) { state.threadId = (int) threadIdGenerator.getAndIncrement(); state.totalThreadCount = (int) totalThreadCount.get(); logger.info("totalThreadCount: " + state.totalThreadCount); logger.info("threadId: " + state.threadId); }
public static int globalLock() { logger.debug("Will get global cluster lock..."); try { IAtomicLong lockNumber = getHazelcastInstance().getAtomicLong(FF_GLOBAL_LOCK_NUMBER_NAME); // int ourLockValue = 0 - DomainClassInfo.getServerId(); do { long currentValue = lockNumber.get(); boolean unlocked = currentValue != FF_GLOBAL_LOCK_LOCKED_VALUE; if (unlocked && lockNumber.compareAndSet(currentValue, FF_GLOBAL_LOCK_LOCKED_VALUE)) { logger.debug("Acquired global cluster lock. ({} -> {})", currentValue, FF_GLOBAL_LOCK_LOCKED_VALUE); return (int) currentValue; // transaction counters fit into an integer } else { logger.debug("Global lock taken. Retrying..."); globalLockIsNotYetAvailable(); } } while (true); } catch (RuntimeException e) { logger.error("Failed to acquire global lock"); throw new TransactionError(e); } }
@Override public void run() { long iteration = 0; while (!testContext.isStopped()) { counter.incrementAndGet(); if (iteration % logFrequency == 0) { log.info(Thread.currentThread().getName() + " At iteration: " + iteration); } if (iteration % performanceUpdateFrequency == 0) { operations.addAndGet(performanceUpdateFrequency); } iteration++; } totalCounter.addAndGet(iteration); }
@Prepare(global = true) public void prepare() { for (int i = 0; i < lockCount; i++) { long key = lockCounter.getAndIncrement(); targetInstance.getLock(getLockId(key)); IAtomicLong account = targetInstance.getAtomicLong(getAccountId(key)); account.set(initialAmount); } }
private void destroy() { this.atomicnumber.destroy(); }
@Override public long getNext() { return id.getAndIncrement(); }
@Override public boolean compareAndSet(long expect, long update) { return atomicNumber.compareAndSet(expect, update); } }
@Override public Long[] nextRange(int size) { Long[] result = new Long[size]; long current = idGen.getAndAdd(size); for (int i=1; i <= size; i++) { result[i] = current + 1; } return result; }
public long get() { if (this.clustered) { return this.hzAtomicLong.get(); } else { return this.atomicLong.get(); } }
public void set(long value) { if (this.clustered) { this.hzAtomicLong.set(value); } else { this.atomicLong.set(value); } } }