Added changes to fix the details
This commit is contained in:
parent
7f19907f51
commit
aaac4e6a95
|
@ -0,0 +1,159 @@
|
|||
package com.baeldung.map.cuncurrenthashmap;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConcurrentHashMapVsSynchronizedMapTest {
|
||||
|
||||
public final static int THREAD_POOL_SIZE = 5;
|
||||
public final static int TEST_ITERATIONS = 5;
|
||||
public final static int TEST_NO_ITEMS = 500000;
|
||||
|
||||
@Test
|
||||
public void randomReadAndWritePerformaceTest_cuncurrentHashMap_faster()
|
||||
throws InterruptedException {
|
||||
// For synchronizedMap
|
||||
Long totalTimeForSynchronizedMap = 0l;
|
||||
Map<String, Integer> slowerMap = Collections
|
||||
.synchronizedMap(new HashMap<String, Integer>());
|
||||
for (int i = 0; i < TEST_ITERATIONS; i++) {
|
||||
totalTimeForSynchronizedMap += performReadAndWriteTest(slowerMap);
|
||||
}
|
||||
Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS;
|
||||
|
||||
// For ConcurrentHashMap Object
|
||||
Long totalTimeForCuncurrentHashMap = 0l;
|
||||
Map<String, Integer> fasterMap = new ConcurrentHashMap<>();
|
||||
for (int i = 0; i < TEST_ITERATIONS; i++) {
|
||||
totalTimeForCuncurrentHashMap += performReadAndWriteTest(fasterMap);
|
||||
}
|
||||
Long avgTimeForCuncurrentHashMap = totalTimeForCuncurrentHashMap / TEST_ITERATIONS;
|
||||
|
||||
Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForCuncurrentHashMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomWritePerformaceTest_cuncurrentHashMap_faster() throws InterruptedException {
|
||||
// For synchronizedMap
|
||||
Long totalTimeForSynchronizedMap = 0l;
|
||||
Map<String, Integer> slowerMap = Collections
|
||||
.synchronizedMap(new HashMap<String, Integer>());
|
||||
for (int i = 0; i < TEST_ITERATIONS; i++) {
|
||||
totalTimeForSynchronizedMap += performWriteTest(slowerMap);
|
||||
}
|
||||
Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS;
|
||||
|
||||
// For ConcurrentHashMap Object
|
||||
Long totalTimeForCuncurrentHashMap = 0l;
|
||||
Map<String, Integer> fasterMap = new ConcurrentHashMap<>();
|
||||
for (int i = 0; i < TEST_ITERATIONS; i++) {
|
||||
totalTimeForCuncurrentHashMap += performWriteTest(fasterMap);
|
||||
}
|
||||
Long avgTimeForCuncurrentHashMap = totalTimeForCuncurrentHashMap / TEST_ITERATIONS;
|
||||
|
||||
Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForCuncurrentHashMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomReadPerformaceTest_cuncurrentHashMap_faster() throws InterruptedException {
|
||||
|
||||
Map<String, Integer> slowerMap = Collections
|
||||
.synchronizedMap(addItems(new HashMap<String, Integer>()));
|
||||
// For synchronizedMap
|
||||
Long totalTimeForSynchronizedMap = 0l;
|
||||
for (int i = 0; i < TEST_ITERATIONS; i++) {
|
||||
totalTimeForSynchronizedMap += performReadTest(slowerMap);
|
||||
}
|
||||
Long avgTimeForSynchronizedMap = totalTimeForSynchronizedMap / TEST_ITERATIONS;
|
||||
|
||||
Map<String, Integer> fasterMap = Collections
|
||||
.synchronizedMap(addItems(new ConcurrentHashMap<String, Integer>()));
|
||||
// For ConcurrentHashMap Object
|
||||
Long totalTimeForCuncurrentHashMap = 0l;
|
||||
new ConcurrentHashMap<>();
|
||||
for (int i = 0; i < TEST_ITERATIONS; i++) {
|
||||
totalTimeForCuncurrentHashMap += performReadTest(fasterMap);
|
||||
}
|
||||
Long avgTimeForCuncurrentHashMap = totalTimeForCuncurrentHashMap / TEST_ITERATIONS;
|
||||
|
||||
Assert.assertTrue(avgTimeForSynchronizedMap > avgTimeForCuncurrentHashMap);
|
||||
}
|
||||
|
||||
private Map<String, Integer> addItems(Map<String, Integer> map) {
|
||||
for (int i = 0; i < TEST_NO_ITEMS; i++) {
|
||||
Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS);
|
||||
map.put(String.valueOf(randNumber), randNumber);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private long performWriteTest(final Map<String, Integer> map) throws InterruptedException {
|
||||
long startTime = System.nanoTime();
|
||||
ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
||||
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
|
||||
exectures.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < TEST_NO_ITEMS; i++) {
|
||||
Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS);
|
||||
map.put(String.valueOf(randNumber), randNumber);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
exectures.shutdown();
|
||||
exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
|
||||
long entTime = System.nanoTime();
|
||||
return (entTime - startTime) / 1000000L;
|
||||
}
|
||||
|
||||
private long performReadAndWriteTest(final Map<String, Integer> map)
|
||||
throws InterruptedException {
|
||||
long startTime = System.nanoTime();
|
||||
ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
||||
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
|
||||
exectures.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < TEST_NO_ITEMS; i++) {
|
||||
Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS);
|
||||
Integer value = map.get(String.valueOf(randNumber));
|
||||
map.put(String.valueOf(randNumber), randNumber);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
exectures.shutdown();
|
||||
exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
|
||||
long entTime = System.nanoTime();
|
||||
return (entTime - startTime) / 1000000L;
|
||||
}
|
||||
|
||||
private long performReadTest(final Map<String, Integer> map) throws InterruptedException {
|
||||
long startTime = System.nanoTime();
|
||||
ExecutorService exectures = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
||||
for (int j = 0; j < THREAD_POOL_SIZE; j++) {
|
||||
exectures.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < TEST_NO_ITEMS; i++) {
|
||||
Integer randNumber = (int) Math.ceil(Math.random() * TEST_NO_ITEMS);
|
||||
Integer value = map.get(String.valueOf(randNumber));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
exectures.shutdown();
|
||||
exectures.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
|
||||
long entTime = System.nanoTime();
|
||||
return (entTime - startTime) / 1000000L;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.map.cuncurrenthashmap;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConcurrentModificationErrorTest {
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenRemoveAndAddOnHashMap_thenCuncurrentModificationError() {
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
map.put(1, "baeldung");
|
||||
map.put(2, "HashMap");
|
||||
Map<Integer, String> synchronizedMap = Collections.synchronizedMap(map);
|
||||
Iterator<Entry<Integer, String>> iterator = synchronizedMap.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
synchronizedMap.put(4, "Modification");
|
||||
iterator.next();
|
||||
}
|
||||
}
|
||||
|
||||
public void whenRemoveAndAddOnCuncurrentHashMap_thenNoError() {
|
||||
Map<Integer, String> map = new ConcurrentHashMap<>();
|
||||
map.put(1, "baeldung");
|
||||
map.put(2, "HashMap");
|
||||
Iterator<Entry<Integer, String>> iterator = map.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
map.put(4, "Modification");
|
||||
iterator.next();
|
||||
}
|
||||
|
||||
Assert.assertEquals(4, map.size());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.map.cuncurrenthashmap;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PerformanceTest {
|
||||
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void whenRemoveAndAddOnHashMap_thenCuncurrentModificationError() {
|
||||
Map<UserId, String> map = new HashMap<>();
|
||||
Map<UserId, String> synchronizedMap = Collections.synchronizedMap(map);
|
||||
long startTime = System.currentTimeMillis();
|
||||
for(int i=0; i<100000; i++) {
|
||||
UserId userId = new UserId(1);
|
||||
synchronizedMap.put(userId, userId.toString());
|
||||
}
|
||||
long endTime = System.currentTimeMillis();
|
||||
long addTimeForSynchronized = endTime-startTime;
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
for(int i=0; i<100000; i++) {
|
||||
UserId userId = new UserId(1);
|
||||
synchronizedMap.get(userId);
|
||||
}
|
||||
endTime = System.currentTimeMillis();
|
||||
long fetchTimeForSynchronized = endTime-startTime;
|
||||
|
||||
Map<UserId, String> map1 = new ConcurrentHashMap<>();
|
||||
startTime = System.currentTimeMillis();
|
||||
for(int i=0; i<100000; i++) {
|
||||
UserId userId = new UserId(1);
|
||||
map1.put(userId, userId.toString());
|
||||
}
|
||||
endTime = System.currentTimeMillis();
|
||||
long addTimeForConcurrent = endTime-startTime;
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
for(int i=0; i<100000; i++) {
|
||||
UserId userId = new UserId(1);
|
||||
map1.get(userId);
|
||||
}
|
||||
endTime = System.currentTimeMillis();
|
||||
long fetchTimeForConcurrent = endTime-startTime;
|
||||
|
||||
System.out.println("ABC");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.map.cuncurrenthashmap;
|
||||
|
||||
public class UserId {
|
||||
|
||||
private int id;
|
||||
|
||||
public UserId(int id) {
|
||||
super();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.id%10;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue