The_N
The_N

Reputation: 23

ExecutorService: how to increase performance

I have a list which contain latitude and longitude values I just traverse this list fetching the lat and longitude and find the weather report for particular Lat and longitude and update the weather report in dB but it takes around 75 sec for 100 times. How can I decrease this time?

ExecutorService executor=Executors.newFixedThreadPool(10);

HttpClient weatherClient=new DefaultHttpClient();
for(LatandlngList value:latandlngList)
{
   Double lat= value.getLat();
   Double lng= value.getLng();
   String URL="http://api.openweathermap.org/data/2.5/weather?lat="+lat+"&lng="+lng
             +"&appid="+appId;

Future<String>future=executor. Submit(() - >findWeatherReport(URL) ;
Updateweatherreport(future.get());


}

private String findWeatherReport(URL){
HttpGet weatherReq=new HttpGet(URL);
HttpResponse weatherresult=weatherClient.execute(weatherReq);
return weatherresult;
} 

Private String Updateweatherreport(weatherreport)
{
//code for update weather report in dB 
return "ok" ;
} 

Upvotes: 1

Views: 405

Answers (1)

M. Deinum
M. Deinum

Reputation: 125038

Instead of what you are doing now

Future<String>future=executor. Submit(() - >findWeatherReport(URL) ;
Updateweatherreport(future.get());

Create a method that does both in 1 go and call that.

private String findAndUpdateWeatherReport(String url) {
  String report = findWeatherReport(url);
  return Updateweatherreport(report);
}

Now in the calling code do

executor.submit(() -> findAndUpdateWeatherReport(URL));

Then after the submits do the following

executor.shutdown();
while (!executor.awaitTermination(500, TimeUnit.MILLISECONDS)) {}

This will shutdown the executor and wait until all tasks have finished and then cleanup the executor.

Upvotes: 1

Related Questions