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(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);
}
}
}
}
分享到:
相关推荐
java单线程多线程clientserver
多线程的FTP客户端 VC++6.0 mfc SOCKET 套接字 多线程 C/C++
这次编译的dll支持多线程,但是要支持多线程内部是有互斥锁的,所以会有性能损失。但是可以通过 S3_配置 () 来自己选择模式:。#SQLITE_配置_单线程 为单线程模式。#SQLITE_配置_多线程 各个线程使用不同的连接和...
C#.NET多线程实例6个(包括多线程基本使用,多线程互斥等全部多线程使用实例),可直接运行
libevent 多线程 HTTP post服务器
适用多线程的SFTP类,支持代理服务器,是spring服务类,无需修改可直接与spring结合使用。 代码经过大量的并发使用验证,稳定可靠。 依赖的jar包如下: <groupId>com.jcraft</groupId> <artifactId>...
java多线程的使用方法如何使用,怎么使用,使用过程中有什么要注意的
C#.NET多线程实例6个(包括多线程基本使用,多线程互斥等全部多线程使用实例)
异步链接,并且放入到线程中去执行,线程池的基础,代码使用两个用户,放入到一个线程中执行,同理,可以多个用户放入到多个线程中执行,为线程池执行websocket client 打下了基础。
多线程例子 演示多线程使用
使用TCPServer编写(多线程)socket服务 http://blog.csdn.net/ghostfromheaven/article/details/8653421
C#.NET多线程实例6个(包括多线程基本使用,多线程互斥等全部多线程使用实例)完整源码
C#.NET多线程实例6个(包括多线程基本使用,多线程互斥等全部多线程使用实例)(十分经典)
提取FFmpeg中H264解码库的代码,支持多线程和MMX、SSE和AVX等优化。
易语言EThread.fne支持库中文名为易语言多线程支持库,本易语言支持库提供对多线程的支持,并通过实现进入许可证机制以避免多线程冲突。易语言EThread.fne支持库为一般支持库,需要易语言系统3.7版本的支持,需要...
打日志用的,经过了windows和linux下的测试,支持多线程,附上了实例,此实例在windows下运行只要加到win32控制台程序里就行了,记得去掉预编译头,在linux下测试,只要make 就行了,makefile已经在压缩包里了