httpClient 多线程
来源:互联网 发布:手机陀螺仪校准软件 编辑:程序博客网 时间:2024/05/16 11:27
- MultiThreadedHttpConnectionManager
- Connection Release
MultiThreadedHttpConnectionManager
The main reason for using multiple theads in HttpClient is to allow the execution of multiple methods at once (Simultaniously downloading the latest builds of HttpClient and Tomcat for example). During execution each method uses an instance of an HttpConnection. Since connections can only be safely used from a single thread and method at a time and are a finite resource, we need to ensure that connections are properly allocated to the methods that require them. This job goes to theMultiThreadedHttpConnectionManager.
To get started one must create an instance of the MultiThreadedHttpConnectionManager and give it to an HttpClient. This looks something like:
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpClient client = new HttpClient(connectionManager);
This instance of HttpClient can now be used to execute multiple methods from multiple threads. Each subsequent call to HttpClient.executeMethod() will go to the connection manager and ask for an instance of HttpConnection. This connection will be checked out to the method and as a result it must also be returned. More on this below inConnection Release.
Options
The MultiThreadedHttpConnectionManager supports the following options:
connectionStaleCheckingEnabledThe connectionStaleCheckingEnabled flag to set on all created connections. This value should be lefttrue except in special circumstances. Consult the HttpConnection docs for more detail. 这个开启的话对性能会有一点点影响 maxConnectionsPerHostThe maximum number of connections that will be created for any particular HostConfiguration. Defaults to 2.maxTotalConnectionsThe maximum number of active connections. Defaults to 20.In general the connection manager makes an attempt to reuse connections for a particular host while still allowing different connections to be used simultaneously. Connection are reclaimed using a least recently used approach.
Connection Release
One main side effect of connection management is that connections must be manually released when no longer used. This is due to the fact that HttpClient cannot determine when a method is no longer using its connection. This occurs because a method's response body is not read directly by HttpClient, but by the application using HttpClient. When the response is read it must obviously make use of the method's connection. Thus, a connection cannot be released from a method until the method's response body is read which is after HttpClient finishes executing the method. The application therefore must manually release the connection by calling releaseConnection() on the method after the response body has been read. To safely ensure connection release HttpClient should be used in the following manner:
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); HttpClient client = new HttpClient(connectionManager);... // and then from inside some thread executing a method GetMethod get = new GetMethod("http://httpcomponents.apache.org/"); try { client.executeMethod(get); // print response to stdout System.out.println(get.getResponseBodyAsStream()); } finally { // be sure the connection is released back to the connection // manager get.releaseConnection(); }
Particularly, notice that the connection is released regardless of what the result of executing the method was or whether or not an exception was thrown. For every call to HttpClient.executeMethod there must be a matching call to method.releaseConnection().
- httpClient 多线程
- 创建多线程的HttpClient
- Android:多线程的HttpClient
- HttpClient 多线程处理
- 多线程下的httpclient
- HTTPClient多线程处理
- HttpClient多线程并发问题
- HttpClient 支持多线程
- httpClient多线程请求
- 多线程httpclient冲突
- Android HttpClient多线程
- HttpClient多线程并发问题
- 多线程HttpClient调用
- httpClient多线程请求
- Httpclient多线程访问
- HttpClient 多线程处理
- HttpClient多线程并发问题
- 封装多线程的HttpClient
- 当修改一些代码时,使用什么编译命令可以最有效率
- 软件架构师的错误
- 技术杂谈:嫁给程序员的好处
- URLClassLoader应用
- EXTjs 怎样遍历树的所有节点获取节点下的值
- httpClient 多线程
- Tiny210 从源代码开始创建Android
- 使用SeaJS实现模块化JavaScript开发
- 不定输入参数函数的编程中需要用到的宏,即注意事项
- C++引用与指针的区别
- NPN和PNP作为开关管的设计技巧以及全系列三极管参数
- Linux/Ubuntu下 静态编译Qt程序
- IOS中使用json
- 黑客帝国并非虚构,世界就是程序代码,人类末日近在咫尺(连载)