Using John W answer I created an implementation that correctly begin the timeout when the task starts its execution. I even write a unit test for it :)
However, it does not suit my needs since some IO operations do not interrupt when Future.cancel()
is called (ie when Thread.interrupt()
is called).
Some examples of IO operation that may not be interrupted when Thread.interrupt()
is called are Socket.connect
and Socket.read
(and I suspect most of IO operation implemented in java.io
). All IO operations in java.nio
should be interruptible when Thread.interrupt()
is called. For example, that is the case for SocketChannel.open
and SocketChannel.read
.
Anyway if anyone is interested, I created a gist for a thread pool executor that allows tasks to timeout (if they are using interruptible operations...): https://gist.github.com/amanteaux/64c54a913c1ae34ad7b86db109cbc0bf