import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils; /**
* An example that performs GETs from multiple threads.
*
*/
public class ClientMultiThreadedExecution { public static void main(String[] args) throws Exception {
// Create an HttpClient with the ThreadSafeClientConnManager.
// This connection manager must be used if more than one thread will
// be using the HttpClient.
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
cm.setMaxTotal(); HttpClient httpclient = new DefaultHttpClient(cm);
try {
// create an array of URIs to perform GETs on
String[] urisToGet = {
"http://hc.apache.org/",
"http://hc.apache.org/httpcomponents-core-ga/",
"http://hc.apache.org/httpcomponents-client-ga/",
"http://svn.apache.org/viewvc/httpcomponents/"
}; // create a thread for each URI
GetThread[] threads = new GetThread[urisToGet.length];
for (int i = ; i < threads.length; i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpclient, httpget, i + );
} // start the threads
for (int j = ; j < threads.length; j++) {
threads[j].start();
} // join the threads
for (int j = ; j < threads.length; j++) {
threads[j].join();
} } finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
} /**
* A thread that performs a GET.
*/
static class GetThread extends Thread { private final HttpClient httpClient;
private final HttpContext context;
private final HttpGet httpget;
private final int id; public GetThread(HttpClient httpClient, HttpGet httpget, int id) {
this.httpClient = httpClient;
this.context = new BasicHttpContext();
this.httpget = httpget;
this.id = id;
} /**
* Executes the GetMethod and prints some status information.
*/
@Override
public void run() { System.out.println(id + " - about to get something from " + httpget.getURI()); try { // execute the method
HttpResponse response = httpClient.execute(httpget, context); System.out.println(id + " - get executed");
// get the response body as an array of bytes
HttpEntity entity = response.getEntity();
if (entity != null) {
byte[] bytes = EntityUtils.toByteArray(entity);
System.out.println(id + " - " + bytes.length + " bytes read");
} } catch (Exception e) {
httpget.abort();
System.out.println(id + " - error: " + e);
}
} } }

ThreadSafeClientConnManager用来支持多线程的使用http client的更多相关文章

  1. C#写文本日志帮助类(支持多线程)

    代码: using System; using System.Configuration; using System.IO; using System.Threading.Tasks; namespa ...

  2. libcurl的封装,支持同步异步请求,支持多线程下载,支持https

    最近在做一个项目,需要用到http get post等 需求分析需要做到同步和异步,异步请求的返回以可选的回调通知的方式进行. 本人以Linux为例,一步一步的来实现. 配置并且编译libcurl我以 ...

  3. 自动更改IP地址反爬虫封锁,支持多线程(转)

    8年多爬虫经验的人告诉你,国内ADSL是王道,多申请些线路,分布在多个不同的电信机房,能跨省跨市更好,我这里写好的断线重拨组件,你可以直接使用. ADSL拨号上网使用动态IP地址,每一次拨号得到的IP ...

  4. 发布支持多线程的PowerShell模块 —— MultiThreadTaskRunner

    应用场景 多线程在需要批量处理一些任务的时候相当有用,也更加有利于充分利用现有计算机的能力.所有主流的开发语言都支持多线程. 默认情况下,PowerShell作为一个脚本语言,是不支持多线程操作的,虽 ...

  5. 使用plenv安装perl,并使其支持多线程

    plenv与pyenv.rbenv等都是同类型软件中非常好用的,这三个软件不仅命名类似,操作方式也相差无几,节约了很多学习的成本,所以非常推荐: 安装使用plenv: git clone git:// ...

  6. JAVA在语言级支持多线程

    进程:任务 任务并发执行是一个宏观概念,微观上是串行的. 进程的调度是有OS负责的(有的系统为独占式,有的系统为共享式,根据重要性,进程有优先级). 由OS将时间分为若干个时间片. JAVA在语言级支 ...

  7. C# 支持多线程

    C# 支持多线程并行执行程序 .一个程序由一个单线程开始,该单线程由CLR和操作系统创建而成,并具有多线程创建额外线程的功能. .创建线程的方法 2.1 通过Thread类来创建线程. ThreadS ...

  8. php不支持多线程怎么办

    PHP 默认并不支持多线程,要使用多线程需要安装 pthread 扩展,而要安装 pthread 扩展,必须使用 --enable-maintainer-zts 参数重新编译 PHP,这个参数是指定编 ...

  9. 支持多线程的Redis6.0来了

    支持多线程的 Redis 6.0 版本于 2020-05-02 终于发布了,为什么 Redis 忽然要支持多线程?如何开启多线程?开启后性能提升效果如何?线程数量该如何设置?开启多线程后会不会有线程安 ...

随机推荐

  1. Process Node.js 进程

    Process 进程 process.argv 是命令行参数数组,第一个元素是node,第二个元素是脚本文件名,从第三个元素开始每个元素是一个运行参数. process.stdout 标准输出流 co ...

  2. 创建自己的yum软件源(以Cloudera Hadoop的安装为例)

    .下载Cloudera Manager安装文件 Cloudera Manager的可以从如下网址获得: http://archive.cloudera.com/cm4/installer/ 这里选择C ...

  3. spring schema自定义

    今天看了一下分布式服务框架的那本书,于是里面提到了spring schema的自定义,于是去简单的了解了一下 参考资源:spring schema扩展: http://www.yihaomen.com ...

  4. SQLite 字符串连接

    对Mysql可以使用CONCAT进行字符串连接, 但使用sqlite时,没有找到相应的方法,后在网上查找后,可以使用||来连接字符串 例: select 'a'||'b'

  5. javascript小练习—点击将DIV变成红色(通过for循环遍历)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. KEIL的ARM编译器对RW和ZI段的一个处理

    按照C编译器编译的结果,一般会产生RO段,RW段,ZI段.RO是程序中的指令和常量,RW是程序中的已初始化全局变量,ZI是程序中的未初始化或初始化为零的全局变量. 那么如下的代码 int aaa; i ...

  7. linux 怎么查找oracle11g的安装目录

    一般来说,/etc/oraInst.loc文件里会记录oracle的路径,如[oracle@ruby ~]$ cat /etc/oraInst.loc inventory_loc=/u01/app/o ...

  8. Java中的枚举类型详解

    枚举类型介绍 枚举类型(Enumerated Type) 很早就出现在编程语言中,它被用来将一组类似的值包含到一种类型当中.而这种枚举类型的名称则会被定义成独一无二的类型描述符,在这一点上和常量的定义 ...

  9. Jmeter 笔记

    Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如静态文件. ...

  10. 一、ThinkPHP的介绍

    一.ThinkPHP的介绍 //了解 MVC M - Model 模型 工作:负责数据的操作 V - View 视图(模板) 工作:负责前台页面显示 编写html代码 C - Controller 控 ...