java - Threading HttpUrlConnection -
i have 10 lists contains location data, each list has 10.000 entries. starting executerservice thread work.
executorservice executor = executors.newfixedthreadpool(locationlocationlist.size()); for(list<location> locationlist: locationlocationlist){ executor.execute(new testthread(locationlist)); }
it takes soo long finish work, in 10 threads. expensive part seems 1 open url , connect it. idea how can tune this?
@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 data", ex); } return ""; }
steps follow:
store result of
getjson()
method based onlatitude
,longitude
.for each call first check in stored map if found there no need call
getjson()
method again.
i hope if latitude
, longitude
repeating.
sample code:
map<string,string> cache = new concurrenthashmap<string,string>(); key (latitude + "_" + longitude) , value (data)
keep in mind cache
using in multi-threading.
Comments
Post a Comment