in what way is java.net.Socket threadsafe?

前端 未结 6 691
挽巷
挽巷 2020-12-10 05:11

I have a Socket that I am both reading and writing to, via BufferedReaders and BufferedWriters. I\'m not sure which operations are okay to do from separate threads. I would

6条回答
  •  猫巷女王i
    2020-12-10 06:12

    Very interesting, the nio SocketChannel writes are synchronized

    http://www.docjar.com/html/api/sun/nio/ch/SocketChannelImpl.java.html

    The old io Socket stuff depends on the OS so you would have to look at the OS native code to know for sure(and that may vary from OS to OS)...

    Just look at java.net.SocketOutputStream.java which is what Socket.getOutputStream returns.

    (unless of course I missed something).

    oh, one more thing, they could have put synchronization in the native code in every JVM on each OS but who knows for sure. Only the nio is obvious that synchronization exists.

提交回复
热议问题