我有大约10个包含位置数据的列表,每个列表有10.000个条目。我正在启动一个executerService来处理所有的工作。
ExecutorService executor = Executors.newFixedThreadPool(locationLocationList.size());
for(List<Location> locationList: locationLocationList){
executor.execute(new TestThread(locationList));
}完成这项工作需要很长时间,即使是在10个线程中。最昂贵的部分似乎是我打开url并连接到它的部分。有没有人知道我该怎么调?
@Override
public void run() {
while(isRunning){
Gson gson = new Gson();
Map<Integer, Data> map= new HashMap<Integer, Data>();
for(Location location: locations){
String data = getJSON(location.getLatitude(), location.getLongitude());
Data data= gson.fromJson(data, Data.class);
map.put(location.getId(), data);
}
weatherDao.setWeatherFast(map);
isRunning = false;
}
}
private String getJSON(double latitude, double longitude) {
HttpURLConnection httpUrlConnection = null;
try {
URL u = new URL("someurl"+latitude+""+longitude);
httpUrlConnection = (HttpURLConnection) u.openConnection();
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setRequestProperty("Content-length", "0");
httpUrlConnection.connect();
int status = httpUrlConnection.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (Exception ex) {
LOGGER.error("Couldnt get Data", ex);
}
return "";
}发布于 2014-04-06 20:05:34
要遵循的步骤:
getJSON()和Latitude的getJSON()方法的结果,每次调用都会先在存储的地图中检查,如果找到了,则不需要再次调用方法。
如果Latitude和Longitude在重复,我希望这会对你有所帮助。
示例代码:
Map<String,String> cache = new ConcurrentHashMap<String,String>();
key (Latitude + "_" + Longitude) and value (data)请记住,cache在多线程中使用。
https://stackoverflow.com/questions/22893205
复制相似问题