hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Julius Davies <juliusdav...@cucbc.com>
Subject Re: Is this a good idea?
Date Thu, 08 Feb 2007 21:38:57 GMT
Hi, Steve,

Depends if "synchronized" is used on any of your Singleton's methods.
If not, then your calls will probably not get serialized.

You are right to be concerned about accidental serialization, but you'll
probably be okay this time.  Just test it out.  An easy way to test is:

in the servlet
======================
public void doGet( HttpServletRequest rq, HttpServletResponse rx )
{
  try {
    Thread.sleep( 10000 );
  } catch ( InterruptedException ie ) {}  
}


in the client
======================
Runnable r = new Runnable() {
  public void run() {
    long now = System.currentTimeMillis();
    MySingleton instance = MySingleton.getInstance();
    instance.doHttpCall();
    long duration = System.currentTimeMillis() - now;
    System.out.println( "I took " + duration + "ms" );
  }
};

new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();
new Thread( r ).start();


If you see results like:

==========================
I took 10028 ms
I took 20121 ms
I took 29994 ms
I took 30001 ms
I took 40014 ms
I took 50512 ms


Then you are accidentally serializing.


yours,

Julius



On Thu, 2007-08-02 at 15:27 -0500, Steve Terrell wrote:
> Folks,
>    I think I am losing an argument at work. A coworker is implementing
> some code that will be used to call a servlet. HttpClient is the tool of
> choice, of course. The code he is writing must handle multiple requests
> in parallel, thus he will be using the MultiThreadedConnectionManager.
> However, he is proposing to channel all his requests through a
> singleton. 
>    I just don't see how that will work. I am convinced the singleton
> will have the effect of serializing the requests, since each request
> will only get processed when the singleton gets a time slice from the
> jvm. Am I nuts for thinking the singleton will be a bottleneck?
> 
> 
> 
> --Steve
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
-- 
Julius Davies, Senior Application Developer, Product Development
T 416-652-0183 | juliusdavies@cucbc.com


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


Mime
View raw message