com.hazelcast.util.counters
Class SwCounter
java.lang.Object
com.hazelcast.util.counters.SwCounter
- All Implemented Interfaces:
- Counter
public abstract class SwCounter
- extends Object
- implements Counter
A Counter
that is made to be used by a single writing thread.
It makes use of the lazy-set 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 to use the AtomicLong.inc; the problem here is that it requires a full fence (so waiting for
store and load buffers to be drained). This is more expensive.
You might wonder why not to 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.
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.hazelcast.util.counters.Counter |
get, inc, inc |
newSwCounter
public static SwCounter newSwCounter()
- Creates a new SwCounter with 0 as initial value.
- Returns:
- the created SwCounter.
newSwCounter
public static SwCounter newSwCounter(int initialValue)
- Creates a new SwCounter with the given initial value.
- Returns:
- the created SwCounter.
Copyright © 2015 Hazelcast, Inc.. All Rights Reserved.