java.io.Serializable
public class ConcurrentCountingMap
extends java.lang.Object
implements java.io.Serializable
ReentrantReadWriteLock
. Only one thread can write in a stripe at a time, but different stripes
can be modified independently and read access can happen concurrently on each stripe.
Keys are sequences of bytes specified as byte-array fragments. Note that when adding a new key (either
by means of put(byte[], int, int, int)
or by means of addTo(byte[], int, int, int)
),
if the specified array fragment is not used as key in the data structure.
Modifier and Type | Class | Description |
---|---|---|
static class |
ConcurrentCountingMap.LockedMap |
|
protected static class |
ConcurrentCountingMap.Stripe |
Constructor | Description |
---|---|
ConcurrentCountingMap() |
Creates a new concurrent counting map with concurrency level equal to
Runtime.availableProcessors() . |
ConcurrentCountingMap(int concurrencyLevel) |
Creates a new concurrent counting map.
|
Modifier and Type | Method | Description |
---|---|---|
int |
addTo(byte[] array,
int delta) |
Adds a value to the counter associated with a given key.
|
int |
addTo(byte[] array,
int offset,
int length,
int delta) |
Adds a value to the counter associated with a given key.
|
int |
get(byte[] array) |
Gets the value of the counter associated with a given key.
|
int |
get(byte[] array,
int offset,
int length) |
Gets the value of the counter associated with a given key.
|
ConcurrentCountingMap.LockedMap |
lock() |
Acquires a locked copy of this map.
|
int |
put(byte[] array,
int value) |
Sets the value associated with a given key.
|
int |
put(byte[] array,
int offset,
int length,
int value) |
Sets the value associated with a given key.
|
public ConcurrentCountingMap()
Runtime.availableProcessors()
.public ConcurrentCountingMap(int concurrencyLevel)
concurrencyLevel
- the number of stripes (it will be forced to be a power of two).public int get(byte[] array)
array
- a byte array.public int get(byte[] array, int offset, int length)
array
- a byte array.offset
- the first valid byte in array
.length
- the number of valid elements in array
.public int addTo(byte[] array, int delta)
array
- a byte array.delta
- a value to be added to the counter associated with the specified key.public int addTo(byte[] array, int offset, int length, int delta)
array
- a byte array.offset
- the first valid byte in array
.length
- the number of valid elements in array
.delta
- a value to be added to the counter associated with the specified key.public int put(byte[] array, int value)
array
- a byte array.value
- a value to be associated with the specified key.public int put(byte[] array, int offset, int length, int value)
array
- a byte array.offset
- the first valid byte in array
.length
- the number of valid elements in array
.value
- a value to be associated with the specified key.public ConcurrentCountingMap.LockedMap lock()
The locked copy has the same method of a ConcurrentCountingMap
, but without
concurrency overhead. It is useful for bulk operations.
All write locks are acquired when this method is called, and
must be released after the bulk operation has completed.
A typical usage is
LockedMap lockedMap; try { lockedMap = concurrentCountingMap.lock(); // bulk operations } finally { lockedMap.unlock(); }