Counterthat is made to be used by a single writing thread. It makes use of the lazySet to provide a lower overhead than a volatile write on X86 systems. The volatile write requires waiting for the store buffer to be drained which isn't needed for the lazySet. This counter does not provide padding to prevent false sharing. You might wonder why not use the AtomicLong.inc. The problem here is that AtomicLong requires a full fence, so there is waiting for store and load buffers to be drained. This is more expensive. You might also wonder why not use the following:
atomicLong.lazySet(atomicLong.get()+1)This causes a lot of syntactic noise due to lack of abstraction. A counter.inc() gives a better clue what the intent is.
|Modifier and Type||Method and Description|
Creates a new SwCounter with 0 as initial value.
Creates a new SwCounter with the given initial value.
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public static SwCounter newSwCounter()
public static SwCounter newSwCounter(long initialValue)
initialValue- the initial value for the SwCounter.
Copyright © 2016 Hazelcast, Inc.. All Rights Reserved.