use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.
the class GridConcurrentLinkedHashMapMultiThreadedSelfTest method testPutGetRemove.
/**
* Test multithreaded put, get, remove operations in concurrent linked hash map.
*
*
* @param clear {@code true} if test should expect clear map at the end.
* @throws Exception If failed.
*/
private void testPutGetRemove(final boolean clear) throws Exception {
info(">>> Test grid concurrent linked hash map iterator...");
final ConcurrentLinkedHashMap<Integer, String> linkedMap = new ConcurrentLinkedHashMap<>();
Collection<Integer> original = new HashSet<>();
final int keyCnt = 10000;
if (!clear)
for (int i = 0; i < keyCnt; i++) {
linkedMap.put(i, "value" + i);
original.add(i);
}
long start = System.currentTimeMillis();
// Updater threads.
IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
Random rnd = new Random();
int iterCnt = 100000;
for (int i = 0; i < iterCnt; i++) {
int key = rnd.nextInt(keyCnt);
if (clear) {
linkedMap.put(key, "value" + key);
linkedMap.get(key);
linkedMap.remove(key);
} else {
linkedMap.get(key);
linkedMap.remove(key);
linkedMap.put(key, "value" + key);
}
}
return null;
}
}, 10, "updater");
fut.get();
Set<Integer> keys = linkedMap.keySet();
if (clear)
assertTrue("Keys must not be in map " + keys, keys.isEmpty());
else {
original.removeAll(keys);
assertTrue("Keys must be in map: " + original, original.isEmpty());
}
info(">>> put get remove test complete [duration = " + (System.currentTimeMillis() - start) + ']');
}
use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.
the class GridConcurrentLinkedHashMapSelfTest method testEvictionInsert.
/**
*
*/
public void testEvictionInsert() {
final int mapSize = 1000;
Map<Integer, String> tst = new ConcurrentLinkedHashMap<>(10, 0.75f, 1, 1000);
Map<Integer, String> map = new LinkedHashMap<Integer, String>(10, 0.75f, false) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
return size() > mapSize;
}
};
for (int i = 0; i < mapSize; i++) {
tst.put(i, "value" + i);
map.put(i, "value" + i);
}
Random rnd1 = new Random();
int iterCnt = 100000;
int keyCnt = 10000;
for (int i = 0; i < iterCnt; i++) {
int key = rnd1.nextInt(keyCnt);
tst.put(key, "value" + key);
map.put(key, "value" + key);
}
Iterator<Map.Entry<Integer, String>> tstIt = tst.entrySet().iterator();
for (Map.Entry<Integer, String> entry : map.entrySet()) {
assertTrue("No enough elements in key set", tstIt.hasNext());
Map.Entry<Integer, String> tstEntry = tstIt.next();
assertEquals("Key mismatch", tstEntry.getKey(), entry.getKey());
assertEquals("Value mismatch", tstEntry.getValue(), entry.getValue());
}
assertEquals("Invalid map size", mapSize, tst.size());
}
Aggregations