We can use socket options to generate a timeout after a read operation blocks for a specified length of time. This is by far the easiest method of handling timeouts.
A call to the java.net.Socket.setSoTimeout() method allows we to specify the maximum amount of time a Socket I/O operation will block before throwing an InterruptedIOException.
This allows you to trap read timeouts, and handle them correctly. If we\'re trying to handle connection timeouts, or if our application must support earlier versions of Java, then another option is the use of threads.
Multi-threaded applications can wait for timeouts, and then perform some action . However, we as a programmer should avoid writing complex multi-threaded code-particularly in our clients. There\'s usually an easy way to encapsulate the complexity of multi-threading, and provide a simple non-blocking I/O version