package com.cv.java.collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Chandra Vardhan
*
*/
public class ConcurrentHashMapVsSynchronizedMap {
public final static int THREAD_POOL_SIZE = 5;
public static Map<String, Integer> hashTableObject = null;
public static Map<String, Integer> synchronizedMapObject = null;
public static Map<String, Integer> concurrentHashMapObject = null;
public static Map<String, Integer> hashMapObject = null;
public static void main(String[] args) throws InterruptedException {
putTest();
getTest();
}
public static void putTest() throws InterruptedException {
hashTableObject = new Hashtable<String, Integer>();
performPutTest(hashTableObject);
synchronizedMapObject = Collections
.synchronizedMap(new HashMap<String, Integer>());
performPutTest(synchronizedMapObject);
concurrentHashMapObject = new ConcurrentHashMap<String, Integer>();
performPutTest(concurrentHashMapObject);
hashMapObject = new HashMap<String, Integer>();
performPutTest(hashMapObject);
}
public static void getTest() throws InterruptedException {
hashTableObject = new Hashtable<String, Integer>();
hashTableObject.put("cv", 1);
hashTableObject.put("sv", 2);
hashTableObject.put("nk", 3);
hashTableObject.put("cv1", 1);
hashTableObject.put("sv1", 2);
hashTableObject.put("nk1", 3);
hashTableObject.put("cv2", 1);
hashTableObject.put("sv3", 2);
hashTableObject.put("nk2", 3);
performGetTest(hashTableObject);
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("cv", 1);
hashMap.put("sv", 2);
hashMap.put("nk", 3);
hashMap.put("cv1", 1);
hashMap.put("sv1", 2);
hashMap.put("nk1", 3);
hashMap.put("cv2", 1);
hashMap.put("sv3", 2);
hashMap.put("nk2", 3);
synchronizedMapObject = Collections.synchronizedMap(hashMap);
performGetTest(synchronizedMapObject);
concurrentHashMapObject = new ConcurrentHashMap<String, Integer>();
concurrentHashMapObject.put("cv", 1);
concurrentHashMapObject.put("sv", 2);
concurrentHashMapObject.put("nk", 3);
concurrentHashMapObject.put("cv1", 1);
concurrentHashMapObject.put("sv1", 2);
concurrentHashMapObject.put("nk1", 3);
concurrentHashMapObject.put("cv2", 1);
concurrentHashMapObject.put("sv3", 2);
concurrentHashMapObject.put("nk2", 3);
performGetTest(concurrentHashMapObject);
HashMap<String, Integer> hashMap2 = new HashMap<String, Integer>();
hashMap2.put("cv", 1);
hashMap2.put("sv", 2);
hashMap2.put("nk", 3);
hashMap2.put("cv1", 1);
hashMap2.put("sv1", 2);
hashMap2.put("nk1", 3);
hashMap2.put("cv2", 1);
hashMap2.put("sv3", 2);
hashMap2.put("nk2", 3);
performGetTest(hashMap2);
}
public static void performPutTest(final Map<String, Integer> map)
throws InterruptedException {
System.out.println("Test started for: " + map.getClass());
long averageTime = 0;
for (int i = 0; i < 5; i++) {
long startTime = System.nanoTime();
ExecutorService thread = Executors
.newFixedThreadPool(THREAD_POOL_SIZE);
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
thread.execute(new Runnable() {
public void run() {
for (int i = 0; i < 500000; i++) {
Integer randomNumber = (int) Math.ceil(Math
.random() * 550000);
// Put value
map.put(String.valueOf(randomNumber), randomNumber);
}
}
});
}
// Make sure executor stops
thread.shutdown();
// Blocks until all tasks have completed execution after a shutdown
// request
thread.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
long entTime = System.nanoTime();
long totalTime = (entTime - startTime) / 1000000L;
averageTime += totalTime;
System.out.println("2500K entried added/retrieved in " + totalTime
+ " ms");
}
System.out.println("For " + map.getClass() + " the average time is "
+ averageTime / 5 + " ms\n");
}
public static void performGetTest(final Map<String, Integer> map)
throws InterruptedException {
System.out.println("Test started for: " + map.getClass());
long averageTime = 0;
for (int i = 0; i < 5; i++) {
long startTime = System.nanoTime();
ExecutorService thread = Executors
.newFixedThreadPool(THREAD_POOL_SIZE);
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
thread.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 500000; i++) {
for (Map.Entry<String, Integer> entry : map
.entrySet()) {
entry.getKey();
entry.getValue();
}
}
}
});
}
// Make sure executor stops
thread.shutdown();
// Blocks until all tasks have completed execution after a shutdown
// request
thread.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
long entTime = System.nanoTime();
long totalTime = (entTime - startTime) / 1000000L;
averageTime += totalTime;
System.out.println("Entried get in " + totalTime
+ " ms");
}
System.out.println("For " + map.getClass() + " the average time is "
+ averageTime / 5 + " ms\n");
}
} |
No comments:
Post a Comment