gpt4 book ai didi

Java Executor服务性能

转载 作者:行者123 更新时间:2023-11-30 04:31:07 24 4
gpt4 key购买 nike

您好,我已经实现了一种方法,可以根据数百万个元素(整数)的数组计算众数值。我现在正在将顺序版本与使用执行器服务的(应该是)改进版本进行比较...不幸的是,性能不如预期:

Sequentiallly iterating hashMap (version 0)

#size #time #memory
10000000 13772ms 565mb
20000000 35355ms 1135mb
30000000 45879ms 1633mb

Assigning jobs to a Service Executor (version 2)
#size #time #memory
10000000 16186ms 573mb
20000000 34561ms 1147mb
30000000 54792ms 1719mb

执行器服务的代码如下:

 /* Optimised-Threaded Method to calculate the Mode */
private int getModeOptimisedThread(int[] mybigarray){
System.out.println("calculating mode (optimised w/ ExecutorService)... ");

int mode = -1;

//create an hashmap to calculating the frequencies
TreeMap<Integer, Integer> treemap = new TreeMap<Integer, Integer>();

//for each integer in the array, we put an entry into the hashmap with the 'array value' as a 'key' and frecuency as 'value'.
for (int i : mybigarray) {
//we check if that element already exists in the Hashmap, by getting the element with Key 'i'
// if the element exists, we increment the frequency, otherwise we insert it with frecuency = 1;
Integer frequency = treemap.get(i);
int value = 0;
if (frequency == null){ //element not found
value = 1;
}
else{ //element found
value = frequency + 1;
}

//insert the element into the hashmap
treemap.put(i, value);
}



//Look for the most frequent element in the Hashmap
int maxCount = 0;

int n_threads = Runtime.getRuntime().availableProcessors();
ExecutorService es = Executors.newFixedThreadPool(n_threads);


//create a common variable to store maxCount and mode values
Result r = new Result(mode, maxCount);

//set the umber of jobs
int num_jobs = 10;
int job_size = treemap.size()/num_jobs;

System.out.println("Map size "+treemap.size());
System.out.println("Job size "+job_size);

//new MapWorker(map, 0, halfmapsize, r);
int start_index, finish_index;

List<Callable<Object>> todolist = new ArrayList<Callable<Object>>(num_jobs);

//assign threads to pool

for (int i=0; i<num_jobs; i++)
{
start_index=i*job_size;
finish_index = start_index+job_size;

System.out.println("start index: "+start_index+". Finish index: "+finish_index);
todolist.add(Executors.callable(new MapWorker(treemap.subMap(start_index, finish_index), r)));

}
try{
//invoke all will not return until all the tasks are completed
es.invokeAll(todolist);
} catch (Exception e) {
System.out.println("Error in the Service executor "+e);
} finally {
//finally the result
mode = r.getMode();
}

//return the result
return mode;
}

关于执行器服务代码的质量有什么建议吗?请提出建议,我是第一次实现E.S.

编辑:

worker 公共(public)类 MapWorker 实现 Runnable{

    private int index;
private int size;
private int maxCount;
private Result result;
private Map <Integer, Integer> map;

//Constructor
MapWorker( Map <Integer, Integer> _map, Result _result){
this.maxCount = 0;
this.result = _result;
map = _map;
}

public void run(){
for (Map.Entry<Integer, Integer> element : map.entrySet()) {
if (element.getValue() > result.getCount()) {
result.setNewMode(element.getKey(),element.getValue());
}
}
}

}

和结果类:

public class Result {
private int mode;
private int maxCount;

Result(int _mode, int _maxcount){
mode = _mode;
maxCount = _maxcount;
}

public synchronized void setNewMode(int _newmode, int _maxcount) {
this.mode = _newmode;
this.maxCount = _maxcount;
}

public int getMode() {
return mode;
}

public synchronized int getCount() {
return maxCount;
}

}

最佳答案

  1. 对于每个作业,使用单独的 Result 对象(不同步)。当所有作业完成后,选择具有最大值的结果。

  2. int num_jobs = n_threads;

关于Java Executor服务性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com