同時Mapのテスト
4349 ワード
/**
* ConcurrentHashMap
*/
public class MapTest {
public static void main(String[] args) throws InterruptedException {
pressureTest(new Hashtable<>(), 5);
pressureTest(Collections.synchronizedMap(new HashMap<>()), 5);
pressureTest(new ConcurrentHashMap<>(),5);
}
private static void pressureTest(Map map , int threshold) throws InterruptedException {
System.out.println("start pressure map [ "+ map.getClass().getName() + " ] use thread size "+ threshold);
long start = System.currentTimeMillis();
for(int i = 0; i<5; i++){
ExecutorService executorService = Executors.newFixedThreadPool(threshold);
for (int j=0; j){
executorService.submit(new Runnable() {
@Override
public void run() {
for(int k=0;k<500000;k++){
int ceil = (int)Math.ceil(Math.random() * 600000);
map.put(ceil, ceil);
}
}
});
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.HOURS);
}
long end = System.currentTimeMillis();
long average = (end-start)/5;
System.out.println("for the map [ "+map.getClass().getName()+" ] , the average time is "+average +" ms");
}
}