Java多线程HTTP

Java多线程HTTP,java,multithreading,http,connection,Java,Multithreading,Http,Connection,我有一个工作程序,它循环遍历UID列表(20000多个项),构建、连接、序列化和保存找到的项属性。很好 我想实现的是加快速度。它必须发出的20000多个HTTP请求以及之后的一切。。它不是特别快 我试着阅读多线程和下面的代码,关于connectionManager。重新使用HttpClient等,但我无法理解或将给定代码应用于我的情况 如何创建代码,使其同时发送多个HTTP请求以加快进程? PoolingHttpClientConnectionManager cm = new PoolingHt

我有一个工作程序,它循环遍历UID列表(20000多个项),构建、连接、序列化和保存找到的项属性。很好

我想实现的是加快速度。它必须发出的20000多个HTTP请求以及之后的一切。。它不是特别快

我试着阅读多线程和下面的代码,关于connectionManager。重新使用HttpClient等,但我无法理解或将给定代码应用于我的情况

如何创建代码,使其同时发送多个HTTP请求以加快进程?

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();
下面是我当前的代码,我如何使这个过程更快

JSONObject httpJSONObject;
            for (int i = 0; i < missingUIDList.size(); i++)
                try {
                    HttpGet get = new HttpGet("https://api.guildwars2.com/v2/items/" + missingUIDList.get(i));
                    HttpClient client = HttpClientBuilder.create().build();
                    HttpResponse response = client.execute(get);
                    HttpEntity entity = response.getEntity();
                    String result = EntityUtils.toString(entity);
                    httpJSONObject = new JSONObject(result);

                    itemRoot items = new Gson().fromJson(httpJSONObject.toString(), itemRoot.class);
                    String name = items.getName().replaceAll("'","''");
                    connection = DriverManager.getConnection("jdbc:sqlite:gw2.db");
                    Statement statement = connection.createStatement();
                    statement.setQueryTimeout(30);  // set timeout to 30 sec.
                    String cookie = "INSERT INTO item VALUES('" + name +
                            "','" + items.getDescription() +
                            "','" + items.getType() +
                            "'," + items.getLevel() +
                            ",'" + items.getRarity() +
                            "'," + items.getVendor_value() +
                            "," + items.getDefault_skin() +
                            "," + items.getId() +
                            ",'" + items.getChat_link() +
                            "','" + items.getIcon() +
                            "');";
                    System.out.println(cookie);
                    statement.executeUpdate(cookie);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    System.err.println(e.getMessage());
                }
        }
JSONObject-httpJSONObject;
for(int i=0;i
编辑:

有了Vadim的提示,这是针对单线程的优化代码,希望更多

private void addMissingItems(List<Integer> missingUIDList) {
    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response;
    HttpEntity entity;
    String result;
    try {
        connection = DriverManager.getConnection("jdbc:sqlite:gw2.db");
        statement = connection.createStatement();
        statement.setQueryTimeout(30);  // set timeout to 30 sec.
    } catch (SQLException e) {
        System.err.println(e.getMessage());
    }

    for (int i = 0; i < missingUIDList.size(); i++)
        try {
            HttpGet get = new HttpGet("https://api.guildwars2.com/v2/items/" + missingUIDList.get(i));
            response = client.execute(get);
            entity = response.getEntity();
            result = EntityUtils.toString(entity);
            JSONObject httpJSONObject = new JSONObject(result);
            itemRoot items = new Gson().fromJson(httpJSONObject.toString(), itemRoot.class);

            System.out.println(httpJSONObject.getInt("id"));
            String cookie = "INSERT INTO item VALUES('" + items.getName().replaceAll("'","''") +
                    "','" + items.getDescription() +
                    "','" + items.getType() +
                    "'," + items.getLevel() +
                    ",'" + items.getRarity() +
                    "'," + items.getVendor_value() +
                    "," + items.getDefault_skin() +
                    "," + items.getId() +
                    ",'" + items.getChat_link() +
                    "','" + items.getIcon() +
                    "');";
            statement.executeUpdate(cookie);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
}
private void addMissingItems(列表missingUIDList){
HttpClient client=HttpClientBuilder.create().build();
HttpResponse响应;
http实体;
字符串结果;
试一试{
connection=DriverManager.getConnection(“jdbc:sqlite:gw2.db”);
statement=connection.createStatement();
语句。setQueryTimeout(30);//将超时设置为30秒。
}捕获(SQLE异常){
System.err.println(e.getMessage());
}
for(int i=0;i
使用

poolighttpclientconnectionmanager cm=new-poolighttpclientconnectionmanager();
CloseableHttpClient httpClient=HttpClients.custom()
.setConnectionManager(cm)
.build();
private final Executor服务池=Executors.newFixedThreadPool(池大小);
for(int i=0;iPoolingHttpClientConnectionManager cm = new   PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(cm)
            .build();

private final ExecutorService pool = Executors.newFixedThreadPool(poolSize);

for (int i = 0; i < missingUIDList.size(); i++) {
    HttpGet get = new HttpGet("https://api.guildwars2.com/v2/items/" + missingUIDList.get(i));
    pool.execute(new Worker(get));
}

class Worker implements Runnable {
    private final HttpGet get;
    private final CloseableHttpClient httpClient;
    Handler(CloseableHttpClient httpClient,HttpGet get) { 
        this.get = get;
        this.httpClient = httpClient;
    }
    public void run() {
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity);
            httpJSONObject = new JSONObject(result);
            ....
            //rest of your code
            ....
            statement.executeUpdate(cookie);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
    }
}
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response;
HttpEntity entity;
String result;
for (int i = 0; i < missingUIDList.size(); i++)
 try {
       HttpGet get = new HttpGet("https://api.guildwars2.com/v2/items/" + missingUIDList.get(i));
       response = client.execute(get);
       entity = response.getEntity();
       result = EntityUtils.toString(entity);
       httpJSONObject = new JSONObject(result);
                ...