ThreadSafeClientConnManager用来支持多线程的使用http client
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的更多相关文章
- C#写文本日志帮助类(支持多线程)
代码: using System; using System.Configuration; using System.IO; using System.Threading.Tasks; namespa ...
- libcurl的封装,支持同步异步请求,支持多线程下载,支持https
最近在做一个项目,需要用到http get post等 需求分析需要做到同步和异步,异步请求的返回以可选的回调通知的方式进行. 本人以Linux为例,一步一步的来实现. 配置并且编译libcurl我以 ...
- 自动更改IP地址反爬虫封锁,支持多线程(转)
8年多爬虫经验的人告诉你,国内ADSL是王道,多申请些线路,分布在多个不同的电信机房,能跨省跨市更好,我这里写好的断线重拨组件,你可以直接使用. ADSL拨号上网使用动态IP地址,每一次拨号得到的IP ...
- 发布支持多线程的PowerShell模块 —— MultiThreadTaskRunner
应用场景 多线程在需要批量处理一些任务的时候相当有用,也更加有利于充分利用现有计算机的能力.所有主流的开发语言都支持多线程. 默认情况下,PowerShell作为一个脚本语言,是不支持多线程操作的,虽 ...
- 使用plenv安装perl,并使其支持多线程
plenv与pyenv.rbenv等都是同类型软件中非常好用的,这三个软件不仅命名类似,操作方式也相差无几,节约了很多学习的成本,所以非常推荐: 安装使用plenv: git clone git:// ...
- JAVA在语言级支持多线程
进程:任务 任务并发执行是一个宏观概念,微观上是串行的. 进程的调度是有OS负责的(有的系统为独占式,有的系统为共享式,根据重要性,进程有优先级). 由OS将时间分为若干个时间片. JAVA在语言级支 ...
- C# 支持多线程
C# 支持多线程并行执行程序 .一个程序由一个单线程开始,该单线程由CLR和操作系统创建而成,并具有多线程创建额外线程的功能. .创建线程的方法 2.1 通过Thread类来创建线程. ThreadS ...
- php不支持多线程怎么办
PHP 默认并不支持多线程,要使用多线程需要安装 pthread 扩展,而要安装 pthread 扩展,必须使用 --enable-maintainer-zts 参数重新编译 PHP,这个参数是指定编 ...
- 支持多线程的Redis6.0来了
支持多线程的 Redis 6.0 版本于 2020-05-02 终于发布了,为什么 Redis 忽然要支持多线程?如何开启多线程?开启后性能提升效果如何?线程数量该如何设置?开启多线程后会不会有线程安 ...
随机推荐
- Ado.net 类扩展属性
.要扩展的类名字一样,2个类加(partial) 小例子: using System; using System.Collections.Generic; using System.Linq; usi ...
- redolog
总结: 教育我们:redo文件组需要创建多个组,建议至少3个,每个组至少2个文件,最好放在不同的硬盘上. 当联机日志文件丢失一般使用清除日志的方法: alter database clear logf ...
- iOS中点击背景收键盘
这一次给大家带来的是ios中点击背景如何收键盘(感觉不错给个赞
- SGU 506.Subsequences Of Substrings
求一个串S有多少子串subS满足s是subS的子序列.len(S)<=100000, len(s)<=100 直接扫一遍... ------------------------------ ...
- Android 修改host文件的3种方法
Android修改hosts文件的方法介绍 本文介绍三种Android手机修改hosts文 件的方法,但修改hosts文件一定要谨慎:Android手机hosts文件的换行符必须是n而不是window ...
- Day 1: How to install jedi/codeintel plugin for sublime on Linux
Step 1, Install sublime3 Download sublime2/3 from http://www.sublimetext.com/ $tar -jxvf sublime_tex ...
- 记录.net 中的常见术语
--Entity Framework和NHibernate --EF和NH都是一种ORM技术.就是对象关系模型映射. --NHibernate和Entity Framework 4.0优劣势争论 -- ...
- python网络编程-01
python网络编程 1.socket模块介绍 ①在网络编程中的一个基本组件就是套接字(socket),socket是两个程序之间的“信息通道”. ②套接字包括两个部分:服务器套接字.客户机套接字 ③ ...
- C#中隐式操作CMD命令行窗口
原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...
- 让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET
让 QtWebkit 支持跨域CROS - nowboy的CSDN博客 - 博客频道 - CSDN.NET 让 QtWebkit 支持跨域CROS 2013-05-23 22:05 450人阅读 评论 ...