@Override public boolean compareAndSet(long expect, long update) { return atomicNumber.compareAndSet(expect, update); } }
private void compare(long expected, Exchange exchange) { long update = exchange.getIn().getBody(Long.class); if (ObjectHelper.isEmpty(expected)) { throw new IllegalArgumentException("Expected value must be specified"); } exchange.getOut().setBody(this.atomicnumber.compareAndSet(expected, update)); }
@ManagedAnnotation(value = "compareAndSet", operation = true) @ManagedDescription("compare expected value with current value if equals then set") public boolean compareAndSet(long expect, long value) { return managedObject.compareAndSet(expect, value); }
@ManagedAnnotation(value = "compareAndSet", operation = true) @ManagedDescription("compare expected value with current value if equals then set") public boolean compareAndSet(long expect, long value) { return managedObject.compareAndSet(expect, value); }
public static void initGlobalLockNumber(int value) { IAtomicLong lockNumber = getHazelcastInstance().getAtomicLong(FF_GLOBAL_LOCK_NUMBER_NAME); lockNumber.compareAndSet(0, value); }
@Override public boolean init(long id) { if (id < 0) { return false; } long step = (id / BLOCK_SIZE); synchronized (this) { boolean init = blockGenerator.compareAndSet(0, step + 1); if (init) { LOCAL.set(this, step); RESIDUE.set(this, (int) (id % BLOCK_SIZE) + 1); } return init; } }
@Override public boolean init(long id) { if (id < 0) { return false; } long step = (id / BLOCK_SIZE); synchronized (this) { boolean init = blockGenerator.compareAndSet(0, step + 1); if (init) { LOCAL.set(this, step); RESIDUE.set(this, (int) (id % BLOCK_SIZE) + 1); } return init; } }
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); } }
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; } } }