5:05 PM
0
A: in what way is java.net.Socket threadsafe?

Op De CirkelJava java.net.Socket is not actually thread safe: Open the Socket source, and look at the (let say) connected member field and how it is used. You will see that is not volatile, read and updated without synchrinization. This indicates that Socket class is not designed to be used by multiple threa...

 
EJP
There's an awful lot of code out there that would break if this was even remotely true. The 'connected' member is only set once in the life of the socket. The basic issue is that the input stream is separate from the output stream and the underlying TCP stream is full duplex: there is no thread-unsafety there.
 
@EJP it is not only connected, another one is created. Do you want to have random behavior because the SocketImpl is not properly initialized when trying to use it?
I don't think that there is a lot of code that accesses single open Socket from different threads. That is because, usually, you have a protocol and you write based on what you've read before and other way around. Doing this in different threads (IMO) is very unusual
>>> the input stream is separate from the output stream If those are different objects, doesn't mean that they do not access the same state of the socket. Browse a little bit the sources of Socket, SocketImpl, PlainSocketImpl, etc and you will see that the streams actually have state in common. The class not being designed for multi-threading (give me java-doc/spec that says it is safe) is reason enough not to do it
>>> There's an awful lot of code Give me an example of some FOSS project that is doing that.
 
EJP
Your questions are futile. Obviously nobody wants random behaviour. The question is whether there will really be any random behaviour. I've never seen any, in 14 years. As I said, there is an awful lot of code that would break if your contention was correct. RMI/Jeri in Jini is a very prominent example of code that reads and writes in separate threads.
 
>>> TCP stream is full duplex: This means that there are separate byte streams for in and out, but does not mean that you can use the socket library (java, libc, etc) in concurrent fashion.
 
EJP
Oh come off it. There is no socket support whatsover in libc so that is completely irrelevant. We are discussing the Java API in java.net. What are all the acquireFD()/releaseFD() calls for it if it isn't intended to be used by multiple threads?
 
5:05 PM
>>> There is no socket support whatsover in libc gnu.org/s/hello/manual/libc/index.html#toc_Sockets
 
EJP
The glibc example uses system calls only: socket(), bind(), connect(), etc, and they are all threadsafe. There is no library to speak of, other than the DNS functions.
 
>> I've never seen any, in 14 years I am not talking about what you've seen, i am talking about what is written in the java.net.Socket & friends
 
EJP
'I don't think that there is a lot of code': We are not talking about what you think either, we are talking about facts. You haven't answered my question about acquiring and releasing FDs and you haven't addressed my very conspicuous counter-example to what you 'don't think'.
 
>>> acquiring and releasing FDs FDs are part of the implementation of PlainSocketImpl and are used only to transfer control to the native code. .... And even the FDs are accessed in inconsistent way as they are not volatile, neither access to them is always synchronized
 
 
1 hour later…
6:18 PM
I would like to stop this discussion without profs of what we are saying, let me summarize what i've sayd and where that comes from:

`java.net.Socket` is not designed to be accessed by multiple threads without proper synchronization in the "client" code:
The reason for that is that `java.net.Socket` has mutable state which doesn't follow _Java Memory Model_ requirements for access by multiple threads.
Examples are:
1. The field `created`. It is not volatile and in `public void connect(SocketAddress endpoint, int timeout)` is read without synchronization,
witout external synchroinzation
 
6:34 PM
Don't you think that all of it is:
1) Confusing
2) Undocumented
3) Sun's implementation specific
?
so it is better to avoid it?