I’m using a callable to open some inputstreams enclosed in finally blocks, which for some annoying reason are throwing premature eof exceptions for reasons i dont care about. i want to give the callable a certain amount of time and then shut it off if its not finished. but how do i also shut off the stuff inside the finally block inside the callable?
For example, in the following code the finally block printing “e” never gets executed.
ExecutorService e = Executors.newSingleThreadExecutor();
Future<Integer> f = e.submit(new Callable<Integer>() {
public Integer call() {
try {
int i = 0;
while(i<1) {
}
} finally {
System.out.println("e");
}
return 4;
}
});
try {
Integer i = f.get(2, TimeUnit.SECONDS);
} catch(Exception ef) {
System.out.println("l");
}
I think you are barking up the wrong tree. If the streams are socket input streams, you should use Socket.setSoTimeout() to enforce a read timeout, or HttpURLConnection.setReadimeout() if they are HTTP streams. Databases have timeouts. Everything that can block in a read that I can think of has a timeout.