public static void main(String[] args) { // Start the Embedded Hazelcast Cluster Member. HazelcastInstance hz = Hazelcast.newHazelcastInstance(); // 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 // Multiply the "counter" by passing it an IFunction counter.alter(new MultiplyByTwo()); //value is now 6 // Display the "counter" value System.out.println("counter: " + counter.get()); // Shutdown this Hazelcast Cluster Member hz.shutdown(); } }
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; } } }