aspose file tools*
The moose likes Threads and Synchronization and the fly likes Is Socket.isConnected() Thread Safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Is Socket.isConnected() Thread Safe?" Watch "Is Socket.isConnected() Thread Safe?" New topic
Author

Is Socket.isConnected() Thread Safe?

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Hi,
I did some subtle reasoning about thread safety and visibility while implementing a TCP socket connection pool using apache.commons.pool library., and I would like to share it with you to check if my thoughts are right.

The objects I am putting into the pool are not plain java Sockets, but wrapper objects around a Socket I called "EVLConnection".

Before returning an object to a client, the pool needs to check if the object is valid or not.

In my case, an "EVLConnection" is valid if and only if the contained Socket is connected, but to store the status of "EVLConnection" I am not relying on Socket.isConnected(), but on an internal volatile boolean that I set when I connect the socket.

I am doing this because, watching the Socket class implementation, nor the status variable "connected" is not volatile, nor is setted in a synchronized block, so I thought that if 2 threads try to check the status of the socket, one could read a stale value of the "connected" boolean.

Do you agree with me ?

I paste the code of my pooled object to be more clear (ignore italian comments):




SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Sorry about all these dumb questions, but is hard to reason about concurrency, even in simple cases.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
I am doing this because, watching the Socket class implementation, nor the status variable "connected" is not volatile, nor is setted in a synchronized block, so I thought that if 2 threads try to check the status of the socket, one could read a stale value of the "connected" boolean.


Are you having a single connection object shared by multiple threads in your Connection Pool implementation? If not,why do you care about the thread safety of the isConnected() method?
Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Well in fact the connection pool never assigns the same connection to the same thread. But in case the object is shared among different thread, I should synchronize them on it right ?

Thanks.
Ajay Saxena
Ranch Hand

Joined: Nov 13, 2006
Posts: 154
Ofcourse there's no guarantee that the same conn object is reassigned to the same thread. However how could a single conn object be shared amongst multiple threads,if the connection pool doesn't explicitly allow that.If the pool does allow sharing of the same conn object across different threads,you definitely need to think about thread safety issues while invoking the socket APIs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Socket.isConnected() Thread Safe?