httpcomponents 学习1--并发多线程GET
package org.apache.http.examples.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.PoolingClientConnectionManager;
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.
PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm.setMaxTotal(100); 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 = 0; i < threads.length; i++) {
HttpGet httpget = new HttpGet(urisToGet[i]);
threads[i] = new GetThread(httpclient, httpget, i + 1);
} // start the threads
for (int j = 0; j < threads.length; j++) {
threads[j].start();
} // join the threads
for (int j = 0; 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);
}
} } }
httpcomponents 学习1--并发多线程GET的更多相关文章
- Java学习之并发多线程理解
1.线程简介: 世间万物会同时完成很多工作,如人体同时进行呼吸.血液循环.思考问题等活动,用户既可以使用计算机听歌也可以使用它打印文件,而这些活动完全可以同时进行,这种思想在Java中称为并发,而将并 ...
- java 并发多线程 锁的分类概念介绍 多线程下篇(二)
接下来对锁的概念再次进行深入的介绍 之前反复的提到锁,通常的理解就是,锁---互斥---同步---阻塞 其实这是常用的独占锁(排它锁)的概念,也是一种简单粗暴的解决方案 抗战电影中,经常出现为了阻止日 ...
- 如何才能够系统地学习Java并发技术?
微信公众号[Java技术江湖]一位阿里Java工程师的技术小站 Java并发编程一直是Java程序员必须懂但又是很难懂的技术内容. 这里不仅仅是指使用简单的多线程编程,或者使用juc的某个类.当然这些 ...
- 如何深入学习Java并发编程?
在讲解深入学习Java并发编程的方法之前,先分析如下若干错误的观点和学习方法. 错误观点1:学习Java编程主要是学习多线程. 这话其实是说明了表面现象,多线程其实还真是并发编程的实现方式,但在实际高 ...
- 孙鑫VC学习笔记:多线程编程
孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modified ...
- java并发多线程显式锁Condition条件简介分析与监视器 多线程下篇(四)
Lock接口提供了方法Condition newCondition();用于获取对应锁的条件,可以在这个条件对象上调用监视器方法 可以理解为,原本借助于synchronized关键字以及锁对象,配备了 ...
- java 并发多线程显式锁概念简介 什么是显式锁 多线程下篇(一)
目前对于同步,仅仅介绍了一个关键字synchronized,可以用于保证线程同步的原子性.可见性.有序性 对于synchronized关键字,对于静态方法默认是以该类的class对象作为锁,对于实例方 ...
- Java并发多线程 - 并发工具类JUC
安全共享对象策略 1.线程限制 : 一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改 2.共享只读 : 一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问, 但是任何线程都 ...
- NDK学习笔记-JNI多线程
前面讲到记录到ffmpeg音视频解码的时候,采用的是在主线程中进行操作,这样是不行的,在学习了POSIX多线程操作以后,就可以实现其在子线程中解码了,也可以实现音视频同步了 简单示例 在native实 ...
随机推荐
- SQLServer2008 行转列2
with a as ( select numb,name,row_number() over( partition by numb order by name desc) rowid from fen ...
- MTK平台缩写
HSPA:High Speed Packet Access smartphone application processor,高速分组接入的智能手机应用程序处理器 META mode:Mobile E ...
- 成功的GIT开发分支模型和策略
详细图文并茂以及git flow工具解释参考: http://danielkummer.github.io/git-flow-cheatsheet/index.zh_CN.html 原文地址:http ...
- linux下使用svn
安装:apt-get install subversion CHECKOUT 将文件checkout到本地目录 svn checkout path(path是服务器上的目录)例如:svn checko ...
- UVa 10250 The Other Two Trees
还是读了很长时间的题,不过题本身很简单. 可以把四棵树想象成正方形的四个顶点,已知两个相对顶点的坐标,求另外两个坐标. 不过,原题可没直接这么说,中间需要一些小证明. 题中说有一个平行四边形然后分别以 ...
- Ios中比较两个日期之间的时间差距
1.比较两个日期之间的时间差距 // 1.日历对象(标识:时区相关的标识) NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIde ...
- PHP5.3, PHP5.4, PHP5.5新特性
PHP 5.3中的新特性 1. 支持命名空间 (Namespace) 2. 支持延迟静态绑定(Late Static Binding) 3. 支持goto语句 4. 支持闭包.Lambda/Anony ...
- Sqoop的使用(Mysql To HBase)
最近需要将mysql的数据整合到HBase中,原本使用MapReduce,自己制作job将mysql的数据导入, 查阅资料过程中,发现了开源工具sqoop(关系性数据库与HDFS,HBASE,HIVE ...
- mac os 系统密码正确的 但是进不了系统
今天开始重新学习C 但是一早起来开机进不了系统 密码明明正确的 无语了,后来打苹果售后电话解决了. 今天记录下如何解决的,以后万一又出现这种问题,至于原因嘛 我也不知道 有可能跟我装的双系统有关系.只 ...
- 【转】使用Python的IDE:Eclipse+PyDev
原文网址:http://www.crifan.com/try_with_python_ide_eclipse_pydev/ 之前已经介绍过了一些基本知识: [整理][多图详解]如何在Windows下开 ...