You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by "KARR, DAVID (ATTSI)" <dk...@att.com> on 2011/05/16 22:08:58 UTC

Confused about guidelines for using HttpClient instance in multiple threads

I must be misunderstanding what I'm reading, but the HttpClient doc seems to have conflicting information about how to use an HttpClient instance from multiple threads.

In section 2.9, "Multithreaded request execution", there's the following statement:

"When equipped with a pooling connection manager such as ThreadSafeClientConnManager, HttpClient can be used to execute multiple requests simultaneously using multiple threads of execution."

In section 5.1, "HttpClient Façade", there is the following statement:

"DefaultHttpClient is thread safe. It is recommended that the same instance of this class is reused for multiple request executions. When an instance of DefaultHttpClient is no longer needed and is about to go out of scope the connection manager associated with it must be shut down by calling the ClientConnectionManager#shutdown() method."

Can someone explain this seeming discrepancy?



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Confused about guidelines for using HttpClient instance in multiple threads

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2012-01-23 at 01:49 -0800, Dvora wrote:
> From performance point of view, isn't it preferred creating new HttpClient
> instance per thread? I'm asking because I saw in the source code a
> synchronized block at AbstractHttpClient.execute() so I guess threads
> sharing the same instance will be blocked at that point, Am I right?
> 
> 

One can indeed reduce thread contention on HttpClient instance by using
a separate instance of HttpClient per thread and get better performance
(assuming one would still use a shared connection pool). However, I
expect the difference in performance to be next to negligible.

Oleg

> olegk wrote:
> > 
> > On Mon, 2011-05-16 at 21:10 +0000, KARR, DAVID (ATTSI) wrote:
> >> > -----Original Message-----
> >> > From: Moore, Jonathan (CIM) [mailto:Jonathan_Moore@Comcast.com]
> >> > Sent: Monday, May 16, 2011 1:15 PM
> >> > To: HttpClient User Discussion
> >> > Subject: Re: Confused about guidelines for using HttpClient instance in
> >> > multiple threads
> >> > 
> >> > All this is trying to say is that you don't need a new
> >> > DefaultHttpClient
> >> > instance for every thread, particularly if you only have one connection
> >> > pool. Multiple threads can use the same DefaultHttpClient to execute
> >> > HTTP
> >> > requests.
> >> > 
> >> > However, when the singleton instance passes out of scope, a clean
> >> > shutdown
> >> > requires also shutting down the ConnectionManager.
> >> > 
> >> > Does that help?
> >> 
> >> Then what's the difference between using the default client connection
> >> manager, as opposed to the ThreadSafeClientConnManager?
> >> 
> > 
> > Both are thread-safe but the default can have only one connection at a
> > time, so requests can be executed sequentially, one by one, whereas
> > ThreadSafeClientConnManager can execute multiple requests over multiple
> > connections concurrently.
> > 
> > ThreadSafeClientConnManager was unfortunately a very bad choice of name
> > for the class. 
> > 
> > Oleg
> > 
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > 
> > 
> > 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Confused about guidelines for using HttpClient instance in multiple threads

Posted by Dvora <ba...@gmail.com>.
>From performance point of view, isn't it preferred creating new HttpClient
instance per thread? I'm asking because I saw in the source code a
synchronized block at AbstractHttpClient.execute() so I guess threads
sharing the same instance will be blocked at that point, Am I right?


olegk wrote:
> 
> On Mon, 2011-05-16 at 21:10 +0000, KARR, DAVID (ATTSI) wrote:
>> > -----Original Message-----
>> > From: Moore, Jonathan (CIM) [mailto:Jonathan_Moore@Comcast.com]
>> > Sent: Monday, May 16, 2011 1:15 PM
>> > To: HttpClient User Discussion
>> > Subject: Re: Confused about guidelines for using HttpClient instance in
>> > multiple threads
>> > 
>> > All this is trying to say is that you don't need a new
>> > DefaultHttpClient
>> > instance for every thread, particularly if you only have one connection
>> > pool. Multiple threads can use the same DefaultHttpClient to execute
>> > HTTP
>> > requests.
>> > 
>> > However, when the singleton instance passes out of scope, a clean
>> > shutdown
>> > requires also shutting down the ConnectionManager.
>> > 
>> > Does that help?
>> 
>> Then what's the difference between using the default client connection
>> manager, as opposed to the ThreadSafeClientConnManager?
>> 
> 
> Both are thread-safe but the default can have only one connection at a
> time, so requests can be executed sequentially, one by one, whereas
> ThreadSafeClientConnManager can execute multiple requests over multiple
> connections concurrently.
> 
> ThreadSafeClientConnManager was unfortunately a very bad choice of name
> for the class. 
> 
> Oleg
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Confused-about-guidelines-for-using-HttpClient-instance-in-multiple-threads-tp31632538p33186920.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Confused about guidelines for using HttpClient instance in multiple threads

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2011-05-16 at 21:10 +0000, KARR, DAVID (ATTSI) wrote:
> > -----Original Message-----
> > From: Moore, Jonathan (CIM) [mailto:Jonathan_Moore@Comcast.com]
> > Sent: Monday, May 16, 2011 1:15 PM
> > To: HttpClient User Discussion
> > Subject: Re: Confused about guidelines for using HttpClient instance in
> > multiple threads
> > 
> > All this is trying to say is that you don't need a new
> > DefaultHttpClient
> > instance for every thread, particularly if you only have one connection
> > pool. Multiple threads can use the same DefaultHttpClient to execute
> > HTTP
> > requests.
> > 
> > However, when the singleton instance passes out of scope, a clean
> > shutdown
> > requires also shutting down the ConnectionManager.
> > 
> > Does that help?
> 
> Then what's the difference between using the default client connection manager, as opposed to the ThreadSafeClientConnManager?
> 

Both are thread-safe but the default can have only one connection at a
time, so requests can be executed sequentially, one by one, whereas
ThreadSafeClientConnManager can execute multiple requests over multiple
connections concurrently.

ThreadSafeClientConnManager was unfortunately a very bad choice of name
for the class. 

Oleg



---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


RE: Confused about guidelines for using HttpClient instance in multiple threads

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: Moore, Jonathan (CIM) [mailto:Jonathan_Moore@Comcast.com]
> Sent: Monday, May 16, 2011 1:15 PM
> To: HttpClient User Discussion
> Subject: Re: Confused about guidelines for using HttpClient instance in
> multiple threads
> 
> All this is trying to say is that you don't need a new
> DefaultHttpClient
> instance for every thread, particularly if you only have one connection
> pool. Multiple threads can use the same DefaultHttpClient to execute
> HTTP
> requests.
> 
> However, when the singleton instance passes out of scope, a clean
> shutdown
> requires also shutting down the ConnectionManager.
> 
> Does that help?

Then what's the difference between using the default client connection manager, as opposed to the ThreadSafeClientConnManager?

> On 5/16/11 4:08 PM, "KARR, DAVID (ATTSI)" <dk...@att.com> wrote:
> 
> >I must be misunderstanding what I'm reading, but the HttpClient doc
> seems
> >to have conflicting information about how to use an HttpClient
> instance
> >from multiple threads.
> >
> >In section 2.9, "Multithreaded request execution", there's the
> following
> >statement:
> >
> >"When equipped with a pooling connection manager such as
> >ThreadSafeClientConnManager, HttpClient can be used to execute
> multiple
> >requests simultaneously using multiple threads of execution."
> >
> >In section 5.1, "HttpClient Façade", there is the following statement:
> >
> >"DefaultHttpClient is thread safe. It is recommended that the same
> >instance of this class is reused for multiple request executions. When
> an
> >instance of DefaultHttpClient is no longer needed and is about to go
> out
> >of scope the connection manager associated with it must be shut down
> by
> >calling the ClientConnectionManager#shutdown() method."
> >
> >Can someone explain this seeming discrepancy?
> >
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> >For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Confused about guidelines for using HttpClient instance in multiple threads

Posted by "Moore, Jonathan (CIM)" <Jo...@Comcast.com>.
All this is trying to say is that you don't need a new DefaultHttpClient
instance for every thread, particularly if you only have one connection
pool. Multiple threads can use the same DefaultHttpClient to execute HTTP
requests.

However, when the singleton instance passes out of scope, a clean shutdown
requires also shutting down the ConnectionManager.

Does that help?

Jon
........
Jon Moore
Comcast Interactive Media






On 5/16/11 4:08 PM, "KARR, DAVID (ATTSI)" <dk...@att.com> wrote:

>I must be misunderstanding what I'm reading, but the HttpClient doc seems
>to have conflicting information about how to use an HttpClient instance
>from multiple threads.
>
>In section 2.9, "Multithreaded request execution", there's the following
>statement:
>
>"When equipped with a pooling connection manager such as
>ThreadSafeClientConnManager, HttpClient can be used to execute multiple
>requests simultaneously using multiple threads of execution."
>
>In section 5.1, "HttpClient Façade", there is the following statement:
>
>"DefaultHttpClient is thread safe. It is recommended that the same
>instance of this class is reused for multiple request executions. When an
>instance of DefaultHttpClient is no longer needed and is about to go out
>of scope the connection manager associated with it must be shut down by
>calling the ClientConnectionManager#shutdown() method."
>
>Can someone explain this seeming discrepancy?
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>For additional commands, e-mail: httpclient-users-help@hc.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org