Class JCasHashMap
The creation of the new JCas cover object can, in turn, run arbitrary user code, which can result in updates to the JCasHashMap which occur before this original update occurs.
In a multi-threaded environment, multiple threads can do a "get" for the same Feature Structure instance. If it's not in the Map, the correct behavior is:
one of the threads adds the new element the other threads wait for the one thread to finish adding, and then return the object that the one thread added.
The implementation works as follows:
1) The JCasHashMap is split into "n" sub-maps. The number is the number of cores, but grows more slowly as the # of cores > 16. This number can be specified, but this is not currently exposed in the tuning parameters Locking occurs on the sub-maps; the outer method calls are not synchronized 2) The number of sub maps is rounded to a power of 2, to allow the low order bits of the hash of the key to be used to pick the map (via masking). 3) a put: 3a) locks 3b) does a find; if found, updates that, if not, adds new value 3c) unlocks 4) a putIfAbsent: 4a) does a find (not under lock) - if found returns that (not under lock) - note: this has a race condition if another thread is updating / changing that slot -- this only happens for unsupported conditions, so is not checked -- changing the type of an existing FS while another thread is accessing the CAS 4b) if not found, locks 4c) does find again 4d) if found, return that and release lock 4e) if not found, eval the creator form and use to set the value, and release lock Note: if eval of creator form recursively calls this, that's OK because sync locks can be recursively gotten and released in a nested way. 5) get does a find, if found, returns that. - if not, get lock, redo search -- if not resized, start find from last spot. else start from beginning. - if found, return that (release lock). if not found return null (release lock)
Supports put(pre-computed-value), putIfAbsent(value-to-be-computed, as an IntSupplier) get
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
final TOP
get
(int key) int
get the approximate size (subject to multithreading inaccuracies)int
static final int
hashInt
(int k1) iterator()
final TOP
final TOP
putIfAbsent
(int key, IntFunction<TOP> creator) void
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
JCasHashMap
public JCasHashMap(int capacity)
-
-
Method Details
-
clear
public void clear() -
putIfAbsent
-
get
- Parameters:
key
- -- Returns:
- the item or null
-
put
- Parameters:
value
- -- Returns:
- previous value or null
-
put
- Parameters:
value
- -key
- -- Returns:
- previous value or null
-
hashInt
public static final int hashInt(int k1) -
getApproximateSize
public int getApproximateSize()get the approximate size (subject to multithreading inaccuracies)- Returns:
- the size
-
showHistogram
public void showHistogram() -
getConcurrencyLevel
public int getConcurrencyLevel() -
iterator
-