I\'m trying to write some code that performs a wait via JNA (e.g. by calling the Kernel32 function WaitForSingleObject), but I\'d also like the wait to finish if Thread.inte
Java supports it via NIO and very few people are aware of, the class in question is abstract but that's no issue:
java.nio.channels.spi.AbstractInterruptibleChannel
. It has 3 methods of interest: begin()
and end()
, those are final, plus that one you have to implement: "protected abstract void implCloseChannel() throws IOException"
The method is going to be called from the thread invoking interrupt(), so be careful.
The use is very simple: call begin before entering the native code and end() upon return. Handling the interruption in implCloseChannel.
Happy coding!