I have a program that’s using the FFMPEG binary libraries via JNI. There is a call to open a URL which is blocking in native code, and I need to allow the user to manually cancel this connection. Sometimes Thread.interrupt() works just like it’s supposed to (if the url I’m connecting to is “udp://{whatever}”). If my URL is “rtp://{whatever}”, Thread.interrupt() doesn’t appear to be honored by the native thread. Thread.kill() doesn’t even seem to do it (and is generally frowned on anyhow).
Does anyone know a different way to interrupt or kill a native thread through JNI?
The native code can regularly check the thread to see if it’s interrupted, just as Java code does.
Another approach is operating-system-specific, but you can send you thread a signal and that signal can set a flag – this may be more straightforward for the JNI-side to check.
All thread-stopping is cooperative – you tell the thread to stop by setting a flag (via
Thread.interrupt()or a flag set in a signal handler – and the thread code checks this often.