As the title says I need a way to stop or interrupt a thread that is blocked wainting on an input from the socket.
Sockets allow to set a timeout via the setSoTimeout()
method. This is the preferred way to stop them blocking.
Otherwise you might want to have a look at the nio
package which allows for non-blocking I/O at the expense of more manual work to manage incoming and outgoing data.
Thread.interrupt() should be the method you're looking for. Be sure that your input-accessing methods also check for InterruptedException and ClosedByInterruptException.
Also look at the corresponding page in the Sun Concurrency Tutorial, as another solution suggests.
You could simply close() the IO stream/socket from another thread. Then, you could check on a volatile boolean field if the resulting IOException is due the close or something else. I think the close might result in an java.net.SocketException: Socket closed
exception.