public class StripedCounters<T extends StripedCounters<T>> extends Object
Each instance of this class in use by the application is backed by an array
of N instances protected by striped locks. You acquire()
an instance
and then directly manipulate the value of the counters on that instance. When
you release()
the instance, the counters MAY be published to the
outer instance visible to your application using
add(StripedCounters)
. This is similar to an eventually consistent
transactional model, except that there are no constraints on the validity of
the updates imposed by this scheme.
If reads will be made against the shared instance without holding a lock,
then the individual counters should be volatile fields, AtomicLong
,
or the like so that updates will be visible each time
add(StripedCounters)
is invoked by release()
will be
published in a timely manner.
Modifier and Type | Field and Description |
---|---|
protected int |
batchSize
#of releases before we post the counters to the parent.
|
protected ReentrantLock[] |
locks |
protected int |
n
|
protected T |
parent |
Constructor and Description |
---|
StripedCounters()
Required public zero argument constructor provides no concurrency
control.
|
StripedCounters(int batchSize)
Create a striped counters object using
numProcessors*2
stripes and the specified batchSize. |
StripedCounters(int nstripes,
int batchSize)
Create a striped counters object.
|
Modifier and Type | Method and Description |
---|---|
T |
acquire()
Acquire a "stripe" counters object.
|
void |
add(T o)
Adds counters to the current counters.
|
void |
clear()
Clear the counter values back to zero.
|
CounterSet |
getCounters()
Return a new, empty
CounterSet . |
void |
release()
Release the counters.
|
T |
subtract(T o)
Returns a new
StripedCounters containing the current counter
values minus the given counter values. |
String |
toString()
Return a serialization of the performance counters.
|
protected int batchSize
protected int n
protected final ReentrantLock[] locks
protected T extends StripedCounters<T> parent
public StripedCounters()
public StripedCounters(int batchSize)
numProcessors*2
stripes and the specified batchSize.batchSize
- The values on the instance stripes will be published to the
outer instance every batchSize release()
s.public StripedCounters(int nstripes, int batchSize)
nstripes
- The #of stripes. Each stripe is protected by a lock. The
stripe is selected by acquire()
based on
Object.hashCode()
. The chances of a contention with a
concurrent thread are (1.0/nstripes)
.batchSize
- The values on the instance stripes will be published to the
outer instance every batchSize release()
s.public final T acquire()
Object.hashCode()
. The chances of a contention with a concurrent
thread are (1.0/nstripes)
.
Note: When using this class you MUST be careful to avoid constructions
which could lead to lock ordering problems. If there are other locks
help, then each acquire()
MAY result in a deadlock with an
acquire()
by another thread depending on which stripe is
selected for each thread. Such deadlocks are obviously path dependent and
difficult to detect.
public final void release()
public void add(T o)
o
- The other counters.public T subtract(T o)
StripedCounters
containing the current counter
values minus the given counter values.o
- The other counters.public void clear()
public CounterSet getCounters()
CounterSet
. This method must be extended
to attach the various performance counters to that CounterSet
.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.