public abstract class SwCounter extends Object implements Counter
Counter
that 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.
One 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.
One 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 |
---|---|
static SwCounter |
newSwCounter()
Creates a new SwCounter with 0 as initial value.
|
static SwCounter |
newSwCounter(long initialValue)
Creates a new SwCounter with the given initial value.
|
public static SwCounter newSwCounter()
public static SwCounter newSwCounter(long initialValue)
initialValue
- the initial value for the SwCounter.Copyright © 2017 Hazelcast, Inc.. All Rights Reserved.