Detecting thread interruption with JNA native wait call (Windows)

后端 未结 2 805
轮回少年
轮回少年 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条回答
  •  Happy的楠姐
    2021-01-15 04:06

    Having found a bit of time to do some more research, I went for a digging expedition in the OpenJDK source code this morning. It turns out that starting with the native implementation was wrong; there's a pure-Java mechanism for doing this.

    The class sun.misc.SharedSecrets has a static method getJavaLangAccess(), which returns an object with a method blockedOn(Thread, sun.nio.ch.Interruptible). This can be used to arrange for Thread.interrupt() to call a method supplied by one of my own objects, at which point I can create my own interruption Event object with which I can ensure waits are terminated as required.

    Doing this introduces dependencies on sun's implementation of the Java class library, but probably less so than digging through the JVM's native state to try to extract an event handle that it uses internally.

提交回复
热议问题