Definitely the ugly hack you have here ...
First of all, thread pool threads are not designed for individual quenching and, as a rule, they should be left until completion, especially not stopped with Thread.stop() , which is not recommended even for regular threads.
Using Thread.stop() , as I said, is never encouraged and usually leaves the thread in an inconsistent state, which is probably the reason that the thread pool does not see the thread as dead. It may not even kill him at all.
Any idea why the native code is hanging? I think the root of your problem is here, not part of the flow stop. Threads should work properly until completion, when possible. Perhaps you can find a better implementation that works correctly (or implement something else if you wrote it).
Change As for point 3, you probably need to declare the link to the current thread as volatile , because you assign it in one thread and read it in another:
private volatile Thread workerThread;
Change 2 . I am starting to realize that your JNI code only performs numerical calculations and does not open any descriptors that may remain in an inconsistent state if the thread is suddenly killed. Can you confirm this?
In this case, let me go against my own advice and tell you that in this case you can safely kill the thread with Thread.stop() . However, I recommend that you use a separate thread instead of a thread pool thread to avoid leaving the thread pool in an inconsistent state (as you mentioned, it does not see the thread as dead). It is also more practical because you don’t have to do all these tricks to stop a thread, because you can just call stop() directly on it from the main thread, unlike thread threads.
Tudor source share