We are using Future
with a timeout to accomplish a task. We get a TimeOutException
when time limit exceeds. From the behavior of thread dump
, I realize that underlying thread continues.
Is it the case? How does it take care of multiple threads roaming around?
What if no IOException
is thrown for the thread which was removed from the pool?
If this is true, what is the way to kill
underlying thread. It keeps on waiting for an external IO
in my case.
A part of thread dump:
Thread 29587: (state = IN_NATIVE)
- java.net.SocketInputStream.socketRead0(java.io.FileDescriptor, byte[], int, int, int) @bci=0 (Compiled frame; information may be imprecise)
- java.net.SocketInputStream.read(byte[], int, int) @bci=84, line=129 (Compiled frame)
- java.io.BufferedInputStream.fill() @bci=175, line=218 (Compiled frame)
- java.io.BufferedInputStream.read1(byte[], int, int) @bci=44, line=258 (Compiled frame)
- java.io.BufferedInputStream.read(byte[], int, int) @bci=49, line=317 (Compiled frame)
- sun.net.www.MeteredStream.read(byte[], int, int) @bci=16, line=116 (Compiled frame)
- java.io.FilterInputStream.read(byte[], int, int) @bci=7, line=116 (Compiled frame)
- sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(byte[], int, int) @bci=4, line=2672 (Compiled frame)
- javax.imageio.stream.FileCacheImageInputStream.readUntil(long) @bci=64, line=121 (Compiled frame)
- javax.imageio.stream.FileCacheImageInputStream.read(byte[], int, int) @bci=69, line=167 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(long, boolean, boolean) @bci=0 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(boolean) @bci=12, line=532 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly() @bci=92, line=277 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(int) @bci=41, line=409 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(int, boolean) @bci=2, line=525 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(int, javax.imageio.ImageReadParam, boolean) @bci=3, line=968 (Compiled frame)
- com.sun.imageio.plugins.jpeg.JPEGImageReader.read(int, javax.imageio.ImageReadParam) @bci=8, line=948 (Compiled frame)
- javax.imageio.ImageIO.read(javax.imageio.stream.ImageInputStream) @bci=55, line=1422 (Compiled frame)
- javax.imageio.ImageIO.read(java.net.URL) @bci=42, line=1374 (Compiled frame)
Once TimeOutException occurs (for any task in the loop), we cancel the tasks like this:
for(Entry<Requests, Future<?>> futureTask : futureTasks.entrySet())
{
Future<?> future = futureTask.getValue();
if(!future.isDone() || future.isCancelled())
{
future.cancel(true);
}
}
Shouldn't it solve the problem?
Please advice.
Thanks in advance.