首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线程化HttpUrlConnection

线程化HttpUrlConnection
EN

Stack Overflow用户
提问于 2014-04-06 19:10:42
回答 1查看 506关注 0票数 0

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

代码语言:javascript
复制
ExecutorService executor = Executors.newFixedThreadPool(locationLocationList.size());

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

完成这项工作需要很长时间,即使是在10个线程中。最昂贵的部分似乎是我打开url并连接到它的部分。有没有人知道我该怎么调?

代码语言:javascript
复制
@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 "";
    }
EN

回答 1

Stack Overflow用户

发布于 2014-04-06 20:05:34

要遵循的步骤:

  • 存储基于getJSON()LatitudegetJSON()方法的结果,每次调用都会先在存储的地图中检查,如果找到了,则不需要再次调用

方法。

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

示例代码:

代码语言:javascript
复制
 Map<String,String> cache = new ConcurrentHashMap<String,String>();

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

请记住,cache在多线程中使用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22893205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档