Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Practice to Use HttpClient in Multithreaded Environment
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p><a href="http://www.opensubscriber.com/message/commons-httpclient-dev@jakarta.apache.org/86045.html" rel="noreferrer">http://www.opensubscriber.com/message/commons-httpclient-dev@jakarta.apache.org/86045.html</a></p> <p>Hence, instead of each thread doing :</p> <pre><code>HttpClient c = new HttpClient(); try { c.executeMethod(method); } catch(...) { } finally { method.releaseConnection(); } </code></pre> <p>We plan to have :</p> <p>[METHOD A]</p> <pre><code>// global_c is initialized once through // HttpClient global_c = new HttpClient(new MultiThreadedHttpConnectionManager()); try { global_c.executeMethod(method); } catch(...) { } finally { method.releaseConnection(); } </code></pre> <p>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? </p> <p>If 10 threads are using global_c, will the other 40 threads be locked?</p> <p>Or would it be better if, in every thread, I create an instance of an HttpClient, but release the connection manager explicitly?</p> <p>[METHOD B]</p> <pre><code>MultiThreadedHttpConnectionManager connman = new MultiThreadedHttpConnectionManager(); HttpClient c = new HttpClient(connman); try { c.executeMethod(method); } catch(...) { } finally { method.releaseConnection(); connman.shutdown(); } </code></pre> <p>Will connman.shutdown() suffer performance issues?</p> <p>May I know which method (A or B) is better, for application using an 50++ threads?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload