小编use*_*079的帖子

并行化许多GET请求

是否有任何有效的方法来并行化Java中的大量GET请求.我有一个200,000行的文件,每个行都需要来自Wikimedia的GET请求.然后我必须将一部分响应写入一个公共文件.我已将下面代码的主要部分粘贴为参考.

while ((line = br.readLine()) != null) {
    count++;
    if ((count % 1000) == 0) {
        System.out.println(count + " tags parsed");
        fbw.flush();
        bw.flush();
    }
    //System.out.println(line);
    String target = new String(line);
    if (target.startsWith("\"") && (target.endsWith("\""))) {
        target = target.replaceAll("\"", "");
    }
    String url = "http://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=xml&rvprop=timestamp&rvlimit=1&rvdir=newer&titles=";
    url = url + URLEncoder.encode(target, "UTF-8");
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("GET");
    //add request header
    //con.setRequestProperty("User-Agent", USER_AGENT);
    int responsecode = con.getResponseCode();
    //System.out.println("Sending 'Get' request to URL: …
Run Code Online (Sandbox Code Playgroud)

java parallel-processing multithreading get httprequest

4
推荐指数
1
解决办法
237
查看次数