线程化HttpUrlConnection

时间:2014-04-06 11:10:42

标签: java threadpool httpurlconnection

我有大约10个包含位置数据的列表,每个列表有10.000个条目。我正在启动一个执行服务来处理所有工作。

ExecutorService executor = Executors.newFixedThreadPool(locationLocationList.size());

for(List<Location> locationList: locationLocationList){
    executor.execute(new TestThread(locationList));
}

完成工作需要太长时间,即使是10个线程。最昂贵的部分似乎是我打开网址并连接到它的那个部分。任何人都知道如何调整它?

@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 "";
    }

1 个答案:

答案 0 :(得分:0)

要遵循的步骤:

  • 根据getJSON()Latitude存储Longitude方法的结果。

  • 对于每次通话,如果找到则首先检查存储的地图,然后无需再次调用getJSON()方法。

如果LatitudeLongitude重复,我希望这会对您有所帮助。


示例代码:

 Map<String,String> cache = new ConcurrentHashMap<String,String>();

 key (Latitude + "_" + Longitude) and value (data)

请注意,cache正在多线程中使用。