public[size=12.0000pt] class[size=12.0000pt] CrunchifyConcurrentHashMapVsSynchronizedMap {
[size=12.0000pt]
public[size=12.0000pt] final[size=12.0000pt] static[size=12.0000pt] int[size=12.0000pt] THREAD_POOL_SIZE = 5;
[size=12.0000pt]
public[size=12.0000pt] static[size=12.0000pt] Map<String, Integer> crunchifyHashTableObject = null;
public[size=12.0000pt] static[size=12.0000pt] Map<String, Integer> crunchifySynchronizedMapObject = null;
public[size=12.0000pt] static[size=12.0000pt] Map<String, Integer> crunchifyConcurrentHashMapObject = null;
[size=12.0000pt]
public[size=12.0000pt] static[size=12.0000pt] void[size=12.0000pt] main(String[] args) throws[size=12.0000pt] InterruptedException {
[size=12.0000pt]
// Test with Hashtable Object
crunchifyHashTableObject = new[size=12.0000pt] Hashtable<>();
crunchifyPerformTest(crunchifyHashTableObject);
[size=12.0000pt]
// Test with synchronizedMap Object
crunchifySynchronizedMapObject = Collections.synchronizedMap(new[size=12.0000pt] HashMap<String, Integer>());
crunchifyPerformTest(crunchifySynchronizedMapObject);
[size=12.0000pt]
// Test with ConcurrentHashMap Object
crunchifyConcurrentHashMapObject = new[size=12.0000pt] ConcurrentHashMap<>();
crunchifyPerformTest(crunchifyConcurrentHashMapObject);
[size=12.0000pt]
}
[size=12.0000pt]
public[size=12.0000pt] static[size=12.0000pt] void[size=12.0000pt] crunchifyPerformTest(final[size=12.0000pt] Map<String, Integer> crunchifyThreads) throws[size=12.0000pt] InterruptedException {
[size=12.0000pt]
System.out.println("Test started for: "[size=12.0000pt] + crunchifyThreads.getClass());
long[size=12.0000pt] averageTime = 0;
for[size=12.0000pt] (int[size=12.0000pt] i = 0; i < 5; i++) {
[size=12.0000pt]
long[size=12.0000pt] startTime = System.nanoTime();
ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
[size=12.0000pt]
for[size=12.0000pt] (int[size=12.0000pt] j = 0; j < THREAD_POOL_SIZE; j++) {
crunchifyExServer.execute(new[size=12.0000pt] Runnable() {
@SuppressWarnings("unused")
@Override
public[size=12.0000pt] void[size=12.0000pt] run() {
[size=12.0000pt]
for[size=12.0000pt] (int[size=12.0000pt] i = 0; i < 500000; i++) {
Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000);
[size=12.0000pt]
// Retrieve value. We are not using it anywhere
Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));
[size=12.0000pt]
// Put value
crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
}
}
});
}
[size=12.0000pt]
// Make sure executor stops
crunchifyExServer.shutdown();
[size=12.0000pt]
// Blocks until all tasks have completed execution after a shutdown request
crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
[size=12.0000pt]
long[size=12.0000pt] entTime = System.nanoTime();
long[size=12.0000pt] totalTime = (entTime - startTime) / 1000000L;
averageTime += totalTime;
System.out.println("2500K entried added/retrieved in "[size=12.0000pt] + totalTime + " ms");
}
System.out.println("For "[size=12.0000pt] + crunchifyThreads.getClass() + " the average time is "[size=12.0000pt] + averageTime / 5[size=12.0000pt] + " ms\n");
}
[size=12.0000pt]}