httpclient java多线程_HttpClient多线程

本篇文章帮大家学习HttpClient多线程,包含了HttpClient多线程使用方法、操作技巧、实例演示和注意事项,有一定的学习价值,大家可以用来参考。

多线程程序包含两个或多个可以并发运行的部分,每个部分可以同时处理不同的任务,从而最佳地利用可用资源。

可以通过编写多线程HttpClient程序来执行来自多个线程的请求。

如果要连续从线程执行多个客户端请求,则需要创建ClientConnectionPoolManager。它维护一个HttpClientConnections池,并提供来自线程的多个请求。

连接管理器根据路由汇集连接。如果管理器具有特定路由的连接,则它通过从池中租用现有连接而不是创建新连接来在这些路由中提供新请求。

按照步骤执行多个线程的请求 -

第1步 - 创建客户端连接池管理器通过实例化PoolingHttpClientConnectionManager类来创建客户端连接池管理器。

PoolingHttpClientConnectionManager connManager = new

PoolingHttpClientConnectionManager();

第2步 - 设置最大连接数使用setMaxTotal()方法设置池中的最大连接数。

//Set the maximum number of connections in the pool

connManager.setMaxTotal(100);

第3步 - 创建ClientBuilder对象通过使用setConnectionManager()方法设置连接管理器来创建ClientBuilder对象,如下所示 -

HttpClientBuilder clientbuilder =

HttpClients.custom().setConnectionManager(connManager);

第4步 - 创建HttpGet请求对象

通过将所需的URI作为参数传递给其构造函数来实例化HttpGet类。

HttpGet httpget1 = new HttpGet("URI1");

HttpGet httpget2 = new HttpGet("URI2");

. . . . . . . . . . . .

第5步 - 实现run方法确保已创建一个类,使其成为一个线程(通过扩展线程类或通过实现Runnable接口)并实现run方法。

public class ClientMultiThreaded extends Thread {

public void run() {

//Run method implementation . . . . . . . . . .

}

}

第6步 - 创建Thread对象

通过实例化上面创建的Thread类(ClientMultiThreaded)来创建线程对象。

将HttpClient对象,相应的HttpGet对象和表示ID的整数传递给这些线程。

ClientMultiThreaded thread1 = new ClientMultiThreaded(httpclient,httpget1, 1);

ClientMultiThreaded thread2 = new ClientMultiThreaded(httpclient,httpget2, 2);

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

第7步 - 启动并加入线程使用start()方法启动所有线程,并使用join()方法加入。

thread1.start();

thread2.start();

. . . . . . . .

thread1.join();

thread2.join();

. . . . . . . . . . . .

第8步 - 运行方法实现

在run方法中,执行请求,检索响应并打印结果。

示例

以下示例演示了从多个线程同时执行HTTP请求。在此示例中,尝试执行来自各种线程的各种请求,并尝试打印状态以及每个客户端读取的字节数。

import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.apache.http.util.EntityUtils;

public class ClientMultiThreaded extends Thread {

CloseableHttpClient httpClient;

HttpGet httpget;

int id;

public ClientMultiThreaded(CloseableHttpClient httpClient, HttpGet httpget,

int id) {

this.httpClient = httpClient;

this.httpget = httpget;

this.id = id;

}

@Override

public void run() {

try{

//Executing the request

CloseableHttpResponse httpresponse = httpClient.execute(httpget);

//Displaying the status of the request.

System.out.println("status of thread "+id+":"+httpresponse.getStatusLine());

//Retrieving the HttpEntity and displaying the no.of bytes read

HttpEntity entity = httpresponse.getEntity();

if (entity != null) {

System.out.println("Bytes read by thread thread "+id+":

"+EntityUtils.toByteArray(entity).length);

}

}catch(Exception e){

System.out.println(e.getMessage());

}

}

public static void main(String[] args) throws Exception {

//Creating the Client Connection Pool Manager by instantiating the PoolingHttpClientConnectionManager class.

PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

//Set the maximum number of connections in the pool

connManager.setMaxTotal(100);

//Create a ClientBuilder Object by setting the connection manager

HttpClientBuilder clientbuilder = HttpClients.custom().setConnectionManager(connManager);

//Build the CloseableHttpClient object using the build() method.

CloseableHttpClient httpclient = clientbuilder.build();

//Creating the HttpGet requests

HttpGet httpget1 = new HttpGet("http://www.jikedaquan.com/");

HttpGet httpget2 = new HttpGet("http://www.kaops.com/");

HttpGet httpget3 = new HttpGet("https://www.qries.com/");

HttpGet httpget4 = new HttpGet("https://in.yahoo.com/");

//Creating the Thread objects

ClientMultiThreaded thread1 = new ClientMultiThreaded(httpclient,httpget1, 1);

ClientMultiThreaded thread2 = new ClientMultiThreaded(httpclient,httpget2, 2);

ClientMultiThreaded thread3 = new ClientMultiThreaded(httpclient,httpget3, 3);

ClientMultiThreaded thread4 = new ClientMultiThreaded(httpclient,httpget4, 4);

//Starting all the threads

thread1.start();

thread2.start();

thread3.start();

thread4.start();

//Joining all the threads

thread1.join();

thread2.join();

thread3.join();

thread4.join();

}

}

执行上面示例代码,得到以下结果:

status of thread 1: HTTP/1.1 200 OK

Bytes read by thread thread 1: 36907

status of thread 2: HTTP/1.1 200 OK

Bytes read by thread thread 2: 13725

status of thread 3: HTTP/1.1 200 OK

Bytes read by thread thread 3: 17319

status of thread 4: HTTP/1.1 200 OK

Bytes read by thread thread 4: 127018

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值