Is there a way to cancel a long running file open operation? Long delays due anti-virus scans, mapped network drives etc.
new FileInputStream("a_file_that_the_antivirus_" +
"will_process_for_minutes_before_returning.jar")
// process the contents...
In the above example, the first line can block, but I don’t know any way to asynchronously cancel that operation (if we got the new inputstream object, cancellation is as easy as in.close(), but this question is not about that case).
Is it possible interrupt the open process – perhaps with NIO – or is there already a mechanism such as a simple Thread.interrupt() to cancel the open?
OS: mostly Windows XP+, Java 6+, the blocking is typically due some larger and deeper jar files – for example, Eclipse’s plugins or my own complex apps.
Suppose that there were no solution (I can’t find one, and I suspect that we’re stuck in OS-level calls) how bad would it be to actually do the open attempt in a worker thread, and eventually timeout waiting for that Thread. You main processing could then continue, while in the backgrounbd that worker thread would eventually complet its open, but immediately close the file again – no one is waiting for it any more.
Yes, it’s going to tie up FD resources, but presumably not large numbers – and anyway you could throttle the number of worker threads to control the impact.