I was wondering what’s the timeout accuracy of the method java.net.Socket.connect(SocketAddress, int timeout)?
Initially I’d thought that it’s built atop java.lang.Object.wait and thus have a 10-15 millisecond error (source),
But after examining the source code, it looks like all it does is to delegate the call to java.net.SocketImpl.connect(SocketaAddress, int).
Does this mean that java.net.Socket.connect(SocketAddress, int) does not use Object.wait and hence is not subject to the 10-15 millisecond error Object.wait has?
which is an abstract method of the
SocketImplclass.The subclass actually implementing it (the system-default
SocketImplimplicitly retrieved fromSocketImplFactory.createSocketImpl()in theSocketconstructor) in turn relies on a native method, so it’s not possible to know the inaccuracy in a platform-independent way.— EDIT (response to comment)
If not using a
Socketsubclass that specifies a customSocketImplvia the protectedSocket(SocketImpl impl)constructor, the standardSocketinstance created by theSocket()constructor uses aSocksSocketImpl(which in turn extendsPlainSocketImpl).SocksSocketImpl.connect(SocketAddress address, int timeout)calls
super.connect(SocketAddress address, int timeout)(PlainSocketImpl.connect(SocketAddress address, int timeout)),which in turn calls
PlainSocketImpl.connectToAddress(InetAddress address, int port, int timeout),which in turn calls
PlainSocketImpl.doConnect(InetAddress address, int port, int timeout),which in turn calls
PlainSocketImpl.socketConnect(InetAddress address, int port, int timeout),which is a private native method, and we don’t know what’s inside 🙂
So no, we’re not relying upon
Object.wait.—
See http://jcs.mobile-utopia.com/jcs/18846_PlainSocketImpl.java and http://jcs.mobile-utopia.com/jcs/31401_SocksSocketImpl.java for source code