Detecting thread interruption with JNA native wait call (Windows)

后端 未结 2 801
轮回少年
轮回少年 2021-01-15 03:12

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

2条回答
  •  [愿得一人]
    2021-01-15 03:39

    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!

提交回复
热议问题