我正在尝试对我的服务进行一些性能测试。所以我为此编写了一个多线程程序。它将以很少的线程并行访问我的服务,然后测量每个线程返回所需的时间。
我进行更新并进入地图的方式将是线程安全的。正确的?因为我发现很难调试这个多线程程序来查看我的程序是否正常工作。谁能帮我解决这个多线程程序
private static ConcurrentHashMap<Long, Long> histogram = new ConcurrentHashMap<Long, Long>();
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 1 * 1000; i++) {
service.submit(new ThreadTask(i, histogram));
}
service.shutdown();
while (!service.isTerminated()) {
}
ThreadTask.report();
}
class ThreadTask implements Runnable {
private int id;
private RestTemplate restTemplate = new RestTemplate();
private String result;
private static ConcurrentHashMap<Long, Long> mapData;
public ThreadTask(int id, ConcurrentHashMap<Long, Long> histogram) {
this.id = id;
this.mapData = histogram;
}
@Override
public void run() {
long start_time = System.currentTimeMillis();
result = restTemplate.getForObject("", String.class);
long difference = (System.currentTimeMillis() - start_time);
Long count = getMethod(mapData, difference);
if (count != null) {
count++;
putMethod(mapData, difference, count);
} else {
putMethod(mapData, difference, Long.valueOf(1L));
}
}
private synchronized void putMethod(ConcurrentHashMap<Long, Long> hg2, long difference, Long count) {
hg2.put(Long.valueOf(difference), count);
}
private synchronized Long getMethod(ConcurrentHashMap<Long, Long> hg2, long difference) {
return hg2.get(difference);
}
public static void report() {
System.out.println(mapData);
}
}
根据以下建议更新了代码 -
private static RestTemplate restTemplate = new RestTemplate();
private static String result = null;
private static ConcurrentHashMap<Long, AtomicLong> histogram = new ConcurrentHashMap<Long, AtomicLong>();
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 1 * 1000; i++) {
service.submit(new ThreadTask(i, histogram));
}
service.shutdown();
while (!service.isTerminated()) {
}
ThreadTask.report();
}
class ThreadTask implements Runnable {
private int id;
private static RestTemplate restTemplate = new RestTemplate();
private String result;
private static ConcurrentHashMap<Long, AtomicLong> hg;
public ThreadTask(int id, ConcurrentHashMap<Long, AtomicLong> histogram) {
this.id = id;
this.hg = histogram;
}
@Override
public void run() {
long start_time = System.currentTimeMillis();
result = restTemplate.getForObject("", String.class);
long difference = (System.currentTimeMillis() - start_time);
final AtomicLong before = hg.putIfAbsent(difference, new AtomicLong(1L));
if (before != null) {
before.incrementAndGet();
}
}
public static void report() {
System.out.println(mapData);
}
}
谁能看一下,让我知道这次我做对了吗?