HttpClient 支持多线程



HttpClient 支持多线程


  1. import org.apache.http.HttpEntity;   
  2. import org.apache.http.HttpResponse;   
  3. import org.apache.http.client.HttpClient;   
  4. import org.apache.http.client.methods.HttpGet;   
  5. import org.apache.http.impl.client.DefaultHttpClient;   
  6. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;   
  7. import org.apache.http.protocol.BasicHttpContext;   
  8. import org.apache.http.protocol.HttpContext;   
  9. import org.apache.http.util.EntityUtils;   
  10.   
  11. /**  
  12.  * An example that performs GETs from multiple threads.  
  13.  *  
  14.  */  
  15. public class ClientMultiThreadedExecution {   
  16.   
  17.     public static void main(String[] args) throws Exception {   
  18.         // Create an HttpClient with the ThreadSafeClientConnManager.   
  19.         // This connection manager must be used if more than one thread will   
  20.         // be using the HttpClient.   
  21.         ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();   
  22.         cm.setMaxTotal(100);   
  23.   
  24.         HttpClient httpclient = new DefaultHttpClient(cm);   
  25.         try {   
  26.             // create an array of URIs to perform GETs on   
  27.             String[] urisToGet = {   
  28.                 "http://hc.apache.org/",   
  29.                 "http://hc.apache.org/httpcomponents-core-ga/",   
  30.                 "http://hc.apache.org/httpcomponents-client-ga/",   
  31.                 "http://svn.apache.org/viewvc/httpcomponents/"  
  32.             };   
  33.   
  34.             // create a thread for each URI   
  35.             GetThread[] threads = new GetThread[urisToGet.length];   
  36.             for (int i = 0; i < threads.length; i++) {   
  37.                 HttpGet httpget = new HttpGet(urisToGet[i]);   
  38.                 threads[i] = new GetThread(httpclient, httpget, i + 1);   
  39.             }   
  40.   
  41.             // start the threads   
  42.             for (int j = 0; j < threads.length; j++) {   
  43.                 threads[j].start();   
  44.             }   
  45.   
  46.             // join the threads   
  47.             for (int j = 0; j < threads.length; j++) {   
  48.                 threads[j].join();   
  49.             }   
  50.   
  51.         } finally {   
  52.             // When HttpClient instance is no longer needed,   
  53.             // shut down the connection manager to ensure   
  54.             // immediate deallocation of all system resources   
  55.             httpclient.getConnectionManager().shutdown();   
  56.         }   
  57.     }   
  58.   
  59.     /**  
  60.      * A thread that performs a GET.  
  61.      */  
  62.     static class GetThread extends Thread {   
  63.   
  64.         private final HttpClient httpClient;   
  65.         private final HttpContext context;   
  66.         private final HttpGet httpget;   
  67.         private final int id;   
  68.   
  69.         public GetThread(HttpClient httpClient, HttpGet httpget, int id) {   
  70.             this.httpClient = httpClient;   
  71.             this.context = new BasicHttpContext();   
  72.             this.httpget = httpget;   
  73.             this.id = id;   
  74.         }   
  75.   
  76.         /**  
  77.          * Executes the GetMethod and prints some status information.  
  78.          */  
  79.         @Override  
  80.         public void run() {   
  81.   
  82.             System.out.println(id + " - about to get something from " + httpget.getURI());   
  83.   
  84.             try {   
  85.   
  86.                 // execute the method   
  87.                 HttpResponse response = httpClient.execute(httpget, context);   
  88.   
  89.                 System.out.println(id + " - get executed");   
  90.                 // get the response body as an array of bytes   
  91.                 HttpEntity entity = response.getEntity();   
  92.                 if (entity != null) {   
  93.                     byte[] bytes = EntityUtils.toByteArray(entity);   
  94.                     System.out.println(id + " - " + bytes.length + " bytes read");   
  95.                 }   
  96.   
  97.             } catch (Exception e) {   
  98.                 httpget.abort();   
  99.                 System.out.println(id + " - error: " + e);   
  100.             }   
  101.         }   
  102.   
  103.     }   
  104.   
  105. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值