A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources.

You can execute requests from multiple threads by writing a multithreaded HttpClient program.

If you want to execute multiple client requests from threads consecutively, you need to create a ClientConnectionPoolManager. It maintains a pool of HttpClientConnections and serves multiple requests from threads.

The connections manager pools the connections based on the route. If the manager has connections for a particular route, then it serves new requests in those routes by leasing an existing connection from the pool, instead of creating a new one.

Follow the steps to execute requests from multiple threads −

Step 1 - Creating the Client Connection Pool Manager

Create the Client Connection Pool Manager by instantiating the PoolingHttpClientConnectionManager class.

  1. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
## Step 2 - Set the maximum number of connections

Set the maximum number of connections in the pool using the setMaxTotal() method.

  1. //Set the maximum number of connections in the poolconnManager.setMaxTotal(100);
## Step 3 - Create a ClientBuilder Object

Create a ClientBuilder Object by setting the connection manager using the setConnectionManager() method as shown below −

  1. HttpClientBuilder clientbuilder =HttpClients.custom().setConnectionManager(connManager);
## Step 4 - Create the HttpGet request objects

Instantiate the HttpGet class by passing the desired URI to its constructor as a parameter.

  1. HttpGet httpget1 = new HttpGet(“URI1”);HttpGet httpget2 = new HttpGet(“URI2”);. . . . . . . . . . . .
## Step 5 - Implementing the run method

Make sure that you have created a class, made it a thread (either by extending the thread class or, by implementing the Runnable interface) and implemented the run method.

  1. public class ClientMultiThreaded extends Thread { public void run() { //Run method implementation . . . . . . . . . . }}
## Step 6 - Create Thread objects

Create thread objects by instantiating the Thread class (ClientMultiThreaded) created above.

Pass a HttpClient object, respective HttpGet object and, an integer representing the ID tothese threads.

  1. ClientMultiThreaded thread1 = new ClientMultiThreaded(httpclient,httpget1, 1);ClientMultiThreaded thread2 = new ClientMultiThreaded(httpclient,httpget2, 2);. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
## Step 7 - Start and join the threads

Start all the threads using start() method and join them using the join method().

  1. thread1.start();thread2.start();. . . . . . . .thread1.join();thread2.join();. . . . . . . . . . . .
## Step 8 - Run method implementation

Within the run method, execute the request, retrieve the response and print the results.

Example

Following example demonstrates the execution of HTTP requests simultaneously from multiple threads. In this example, we are trying to execute various requests from various threads and trying to print the status, and the number of bytes read by each client.

  1. import org.apache.http.HttpEntity;
  2. import org.apache.http.client.methods.CloseableHttpResponse;
  3. import org.apache.http.client.methods.HttpGet;
  4. import org.apache.http.impl.client.CloseableHttpClient;
  5. import org.apache.http.impl.client.HttpClientBuilder;
  6. import org.apache.http.impl.client.HttpClients;
  7. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  8. import org.apache.http.util.EntityUtils;
  9.  
  10. public class ClientMultiThreaded extends Thread {
  11. CloseableHttpClient httpClient;
  12. HttpGet httpget;
  13. int id;
  14.  
  15. public ClientMultiThreaded(CloseableHttpClient httpClient, HttpGet httpget,
  16. int id) {
  17. this.httpClient = httpClient;
  18. this.httpget = httpget;
  19. this.id = id;
  20. }
  21. @Override
  22. public void run() {
  23. try{
  24. //Executing the request
  25. CloseableHttpResponse httpresponse = httpClient.execute(httpget);
  26.  
  27. //Displaying the status of the request.
  28. System.out.println("status of thread "+id+":"+httpresponse.getStatusLine());
  29.  
  30. //Retrieving the HttpEntity and displaying the no.of bytes read
  31. HttpEntity entity = httpresponse.getEntity();
  32. if (entity != null) {
  33. System.out.println("Bytes read by thread thread "+id+":
  34. "+EntityUtils.toByteArray(entity).length);
  35. }
  36. }catch(Exception e) {
  37. System.out.println(e.getMessage());
  38. }
  39. }
  40.  
  41. public static void main(String[] args) throws Exception {
  42.  
  43. //Creating the Client Connection Pool Manager by instantiating the PoolingHttpClientConnectionManager class.
  44. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
  45.  
  46. //Set the maximum number of connections in the pool
  47. connManager.setMaxTotal(100);
  48.  
  49. //Create a ClientBuilder Object by setting the connection manager
  50. HttpClientBuilder clientbuilder = HttpClients.custom().setConnectionManager(connManager);
  51.  
  52. //Build the CloseableHttpClient object using the build() method.
  53. CloseableHttpClient httpclient = clientbuilder.build();
  54.  
  55. //Creating the HttpGet requests
  56. HttpGet httpget1 = new HttpGet("http://www.tutorialspoint.com/");
  57. HttpGet httpget2 = new HttpGet("http://www.google.com/");
  58. HttpGet httpget3 = new HttpGet("https://www.qries.com/");
  59. HttpGet httpget4 = new HttpGet("https://in.yahoo.com/");
  60.  
  61. //Creating the Thread objects
  62. ClientMultiThreaded thread1 = new ClientMultiThreaded(httpclient,httpget1, 1);
  63. ClientMultiThreaded thread2 = new ClientMultiThreaded(httpclient,httpget2, 2);
  64. ClientMultiThreaded thread3 = new ClientMultiThreaded(httpclient,httpget3, 3);
  65. ClientMultiThreaded thread4 = new ClientMultiThreaded(httpclient,httpget4, 4);
  66.  
  67. //Starting all the threads
  68. thread1.start();
  69. thread2.start();
  70. thread3.start();
  71. thread4.start();
  72.  
  73. //Joining all the threads
  74. thread1.join();
  75. thread2.join();
  76. thread3.join();
  77. thread4.join();
  78. }
  79. }

Output

On executing, the above program generates the following output −

  1. status of thread 1: HTTP/1.1 200 OK
  2. Bytes read by thread thread 1: 36907
  3. status of thread 2: HTTP/1.1 200 OK
  4. Bytes read by thread thread 2: 13725
  5. status of thread 3: HTTP/1.1 200 OK
  6. Bytes read by thread thread 3: 17319
  7. status of thread 4: HTTP/1.1 200 OK
  8. Bytes read by thread thread 4: 127018