在多线程环境中使用 HttpClient 的最佳实践
一段时间以来,我一直在多线程环境中使用 HttpClient。 对于每个线程,当它发起连接时,都会创建一个全新的 HttpClient 实例。
最近我发现,使用这种方法会导致用户打开的端口过多,并且大部分连接处于TIME_WAIT状态。
http://www.opensubscriber.com/message /[email protected]/86045.html
因此,而不是每个线程执行:
HttpClient c = new HttpClient();
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
我们计划:
[方法 A]
// global_c is initialized once through
// HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager());
try {
global_c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
正常情况下,global_c将被50++个线程同时访问。 我想知道,这会产生任何性能问题吗? MultiThreadedHttpConnectionManager是否使用无锁机制来实现其线程安全策略?
如果有10个线程使用global_c,其他40个线程会被锁定吗?
或者,如果在每个线程中创建 HttpClient 的实例,但显式释放连接管理器,会更好吗?
[方法 B]
MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager();
HttpClient c = new HttpClient(connman);
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
connman.shutdown();
}
connman.shutdown() 会遇到性能问题吗?
我可以知道对于使用 50++ 线程的应用程序,哪种方法(A 或 B)更好?
For a while, I have been using HttpClient in a multithreaded environment. For every thread, when it initiates a connection, it will create a completely new HttpClient instance.
Recently, I have discovered that, by using this approach, it can cause the user to have too many ports being opened, and most of the connections are in TIME_WAIT state.
http://www.opensubscriber.com/message/[email protected]/86045.html
Hence, instead of each thread doing :
HttpClient c = new HttpClient();
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
We plan to have :
[METHOD A]
// global_c is initialized once through
// HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager());
try {
global_c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
}
In a normal situation, global_c will be accessed by 50++ threads concurrently. I was wondering, will this create any performance issues? Is MultiThreadedHttpConnectionManager using a lock-free mechanism to implement its thread safe policy?
If 10 threads are using global_c, will the other 40 threads be locked?
Or would it be better if, in every thread, I create an instance of an HttpClient, but release the connection manager explicitly?
[METHOD B]
MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager();
HttpClient c = new HttpClient(connman);
try {
c.executeMethod(method);
}
catch(...) {
}
finally {
method.releaseConnection();
connman.shutdown();
}
Will connman.shutdown() suffer performance issues?
May I know which method (A or B) is better, for application using an 50++ threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(5)
方法A是httpclient开发者社区推荐的。
请参考 http://www.mail-archive.com /[电子邮件受保护]/msg02455.html 了解更多详细信息。
我对文档的阅读是,HttpConnection 本身不被视为线程安全,因此 MultiThreadedHttpConnectionManager 提供了一个可重用的 HttpConnection 池,您有一个由所有线程共享的 MultiThreadedHttpConnectionManager 并只初始化一次。 因此,您需要对选项 A 进行一些小的改进。
MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManag
然后每个线程应该对每个请求使用该序列,从池中获取一个连接并在完成其工作后将其放回原处 - 使用finally 块可能会很好。
您还应该针对池没有可用连接的可能性进行编码并处理超时异常。
HttpConnection connection = null
try {
connection = connman.getConnectionWithTimeout(
HostConfiguration hostConfiguration, long timeout)
// work
} catch (/*etc*/) {/*etc*/} finally{
if ( connection != null )
connman.releaseConnection(connection);
}
当您使用连接池时,您实际上不会关闭连接,因此这不应该遇到 TIME_WAIT 问题。 这种方法确实假设每个线程不会长时间挂起连接。 请注意,conman 本身保持打开状态。
我想你会想使用 ThreadSafeClientConnManager。
您可以在这里查看它的工作原理: http:// foo.jasonhudgins.com/2009/08/http-connection-reuse-in-android.html
或者在内部使用它的 AndroidHttpClient
中。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
肯定是方法 A,因为它是池化且线程安全的。
如果您使用的是httpclient 4.x,则连接管理器称为ThreadSafeClientConnManager。 请参阅此链接了解更多详细信息(向下滚动到“池连接管理器”)。 例如:
Definitely Method A because its pooled and thread safe.
If you are using httpclient 4.x, the connection manager is called ThreadSafeClientConnManager. See this link for further details (scroll down to "Pooling connection manager"). For example: