wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes HttpUrlConnection called by multiple threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "HttpUrlConnection called by multiple threads" Watch "HttpUrlConnection called by multiple threads" New topic
Author

HttpUrlConnection called by multiple threads

Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
Hi,

Is HttpUrlConnection thread safe? I.e. if I have an HttpConnection instance connected to a server and this instance is used by different threads (e.g.try to send a POST concurrently) how will this situation be handled by HttpUrlConnection? a) Will they send the POSTs serially, or b) the first thread sends the POST, gets the response and then the second thread will send the POST? If they send the POSTs serially, this means multiple active POSTs to the same tcp connection. Is this allowed? Can it be handled by a server?

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
The javadocs state "Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances." While that's not quite clear on the matter of thread-safety, it suggests to me that it should not be shared between threads.


Ping & DNS - my free Android networking tools app
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
Hi,

Yes I have read this and could not make much of this statement also.
My main problem is whether it is possible to "push" multiple POST requests over the same tcp connection for performance reasons.
I was thinking maybe the httpurlconnection already handles this
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
Well, sending several requests over the same connection, and using an HttpURLConnection multi-threaded are two different questions. The former is possible through the HTTP keep-alive feature.
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
Keep alive is just an indication to the server not to close the connection.
My problem is whether it is possible to "push" multiple active POSTs (i.e. without waiting for the response for each POST, eg POST1,POST2 and then get response for POST1, response from POST2) over the same tcp connection.
I was wondering if this is effectively the outcome of sharing an httpurlconnection among threads
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
No, that's not possible. Before the first response has come back you can't send the next request. That's a feature of HTTP, and not any particular implementation.

What prompted this question - trying to maximize throughput, i.e., the number for requests per time unit?
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
I have to implement a web service client with high performance requirements. I was thinking to keep persistent connections to avoid 3-way handshake overhead and was wondering if it would be possible to multiplex the requests over the same connection. The reason for this was that if i opened a new connections for each request, I would hit a limit at the server and if this limit was low (e.g. 2 connections for the same client) then the threads would block.
I suspected that it is not possible, could not found it stated explicitly somewhere though. Do you have an explicit reference Ulf?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
Section 1.4 of the HTTP spec talks about the request/response semantics of HTTP; that may be as close as it gets to an explicit statement.

Of course, that only applies to single connections. Servlet containers -on which most WS implementations are based- can handle parallel independent connections, so if you were to use several HttpUrlConnections for the same URL, you should be able to increase throughput. The "the underlying network connection to the HTTP server may be transparently shared by other instances" part sounds a bit weird in that context, but I doubt that's what will happens if you have multiple threads. A test will tell, though.
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
It seems that HTTP1.1 allows pipelining (8.1.2.2 Pipelining from RFC2616). Clients SHOULD not send POSTs pipelined
I am not sure if the shared use of httpurlconnection though will have the desired result
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42631
    
  65
As an aside, please BeForthrightWhenCrossPostingToOtherSites, on java.net or elsewhere; it's the polite thing to do.
Jim Akmer
Ranch Hand

Joined: Jul 06, 2010
Posts: 104
ok ulf. Well I apologise to you since you were interested in helping me. As an aside, I really needed info on this. Your responses were fine.
I will close this thread as answered.
Glen Divers
Ranch Hand

Joined: Jan 21, 2010
Posts: 61
If you're looking for a high performance socket framework, you might be interested in JBoss Netty. They have some well worked examples of both HTTP servers and clients.

HTH
 
wood burning stoves
 
subject: HttpUrlConnection called by multiple threads