Let’s say I started a thread and I have something like this:
...//initiate all the socket connection
future = executor.submit
( new Runnable()
{ public void run()
{ ...
...
while ((str = in.readLine()) != null)
{ //do something here
}
}
);
executor is a ExecutorService object and in is a BufferedReader object
I know you can close the socket from a different thread to interrupt this thread. But when I try to use future.cancel(true) method, even though it returns true, the thread seems still running, anybody know why? or in.readLine() cannot be interrrupted this way?
All
future.cancel(true)does is to callthread.interrupt()on the associated thread. This will causesleep()andwait()operations to throw anInterruptedExceptionand will interrupt some special NIO channels.But chances are your
BufferedReaderwill not be interrupted since it is most likely reading from a “normal” socket or file. As you mention, closing the underlying socket from a different thread is the best way to kill such an IO method.