hashmap에서 데이터를 읽는 중 확장 성 문제가 발생합니다. 내 컴퓨터에는 코어 당 2 개의 하이퍼 스레드 (총 64cpus)와 64GB RAM이있는 32 코어가 있습니다. HashMap에서 데이터를 읽고 산술 계산을 수행 할 때 16 스레드 이후의 성능 저하를 볼 수 있지만 산술 연산 만 수행하면 예상대로 확장됩니다. arithematic 작업의 HashMap에서 읽고 수행다중 스레드 멀티 코어 시스템에서 HashMap의 확장 성 문제
:
아래의 테스트 결과를 찾아주세요
스레드 없음 | 소요 시간 (초) => 1 | 85, 2 | 93, 4 | 124, 8 | 147, 16 | 644
수행에만 arithematic 작업 :
스레드 없음 | 소요 시간 (초) => 1 | 25, 2 | 32, 4 | 35, 8 | 41, 16 | 65, 32 | 108, 40 | 112, 64 | 117, 100 | 158
또한 참조 코드 블록을 추가 :
import java.util.*;
import java.util.concurrent.*;
import java.lang.*;
public class StringCallable2
{
// private static final long size = 500000L;
private static final long size = 1000000L;
// private final static HashMap <Long,Long>map = new HashMap<Long, Long>();
// private static long[] array = new long[(int) size];
public static class StringGenCallable implements Callable
{
int count;
public StringGenCallable(int count)
{
this.count = count;
}
public Long call()
{
//Random rand = new Random();
// System.out.println("Thread " + count + " started test");
long sum = 20;
// do a CPU intensive arithmetic operation; no Input Output
// operations, object creations or floating point arithmetic
for (long i = 0; i < size; i++)
{
//int numNoRange = rand.nextInt((int)(size-1));
//long numNoRange = i;
// Long long1 = map.get((long)i);
//Long long1 = array[(int)i];
sum = i + 19 * sum;
}
// System.out.println("Finished " + count);
return sum;
}
}
public static void main(String args[])
{
try
{
System.out.println("Starting");
// for (long i = 0; i < size; i++)
// {
//array[(int)i] = System.currentTimeMillis();
// map.put(i, System.currentTimeMillis());
// }
int sizt = Integer.valueOf(args[0]);
long curtime = System.currentTimeMillis();
ExecutorService pool = Executors.newFixedThreadPool(sizt);
Set<Future<Integer>> set = new HashSet<Future<Integer>>();
for (int i = 0; i < sizt; i++)
{
Callable<Integer> callable = new StringGenCallable(i);
Future<Integer> future = pool.submit(callable);
set.add(future);
}
long sum = 0;
for (Future<Integer> future : set)
{
future.get();
}
System.out.println("Number of threads : "+sizt);
long finsihtime = System.currentTimeMillis();
System.out.println("Total Time Taken : " + (finsihtime - curtime)+" ms");
pool.shutdown();
// System.exit(sum);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
catch (Error e) {
// TODO: handle exception
e.printStackTrace();
}
catch (Throwable e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Ooops. 너는 그 질문을 잊었다. – aioobe
그리고 여기서 질문은 무엇입니까? 잠금 경합이 확장 성을 해치는 것은 잘 알려진 사실입니다. 어쨌든, 당신의 경우 다중 스레드 사용을 위해 최적화 된'ConcurrentHashMap'을 시도 할 수 있습니다. –
java5 +를 사용하는 경우 java.util.ConcurrentHashMap을 시도하십시오.이 클래스는 더 적합합니다 – blob