Search code examples
javamultithreadingexecutorserviceobjectinputstream

stop thread that does not get interrupted


I have a thread that sits and reads objects off of an ObjectInputStream:

public void run() {
    try {
        ois = new ObjectInputStream(clientSocket.getInputStream());

        Object o;
        while ((o = ois.readObject()) != null) {
            //do something with object
        }
    } catch (Exception ex) {
        //Log exception
    }
}

readObject does not throw InterruptedException and as far as I can tell, no exception is thrown when this thread is interrupted. How do I stop this thread?


Solution

  • Call close on the socket input stream. You should be able to do that from another thread.