5

Here I want to stop my thread or kill my thread which is created on Java layer, and this thread is calling JNI function. Sometimes as per my application requirement, I have to stop this JNI function execution on some conditions if its going on, otherwise not.

new Thread(new Runnable() {
    @Override
    public void run() {
         // My jni function call, It calls my JNI layer C function.
         }
   }

Now when this thread execution is started and its doing work at JNI level I have no worries about that, but from other class or methods on some condition I want to stop this JNI work so how can I stop this thread.

Note: Here my thread also has no while-loop so I cant check with some global flag variable also.

So does anyone have an idea on how to kill a thread while its call any JNI function without while loop.

Lii
  • 11,553
  • 8
  • 64
  • 88
sam_k
  • 5,983
  • 14
  • 76
  • 110
  • Have you considered Thread.inturrupt() ? – Mr.Me Feb 07 '13 at 09:32
  • @Mr.Me No i didnt consider but how can i check this, it gives me always its not inturrupt – sam_k Feb 07 '13 at 09:37
  • Please take a look at http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html you can stop/destroy or inturrupt a thread by calling the function you want – Mr.Me Feb 07 '13 at 09:42
  • @Mr.Me Ya i know about this all but here my thread calls jni function so how can stop this jni work from java layer. – sam_k Feb 07 '13 at 09:50
  • possible duplicate of [Terminate a thread which is running a native code](http://stackoverflow.com/questions/8762185/terminate-a-thread-which-is-running-a-native-code) – Philipp Wendler Jun 07 '15 at 11:27

3 Answers3

12

You can't safely interrupt a thread, if it is executing a native code. Even if your thread had an event loop, you need to wait until it finishes the native call. Knowing nothing about your code, i would guess that you had a long running native call, you didn't want it to clog the main thread, so you created a separate thread for that call. There is no way you can safely interrupt a single native call. No silver bullet here. You need to change your code in any case, my suggestions would be:

  • decompose your single long native call into series of short calls and run event loop on Java side
  • decompose the native call internally on the native side and run the event loop on native side. Your native interface will need another method for setting the interruption flag.

Thread.interrupt() won't help you because "calling native function" doesn't fall under any of the interruptible actions specified in Javadoc. The Java thread will keep running, only it's interrupt status will be set.

Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
  • Thanks for Answer but its late answer, i already implement interruption of thread using your 2nd way, Great suggestion by the way. and till now your answer is very good among of others answers. – sam_k Feb 13 '13 at 03:50
  • If you feel it's "very good", vote it up so it's more visible and relevant for the future wisdom seekers – Pavel Zdenek Feb 13 '13 at 11:24
  • I am from the future and can confirm: wisdom was sought. – Sipty Jan 29 '16 at 15:46
2

Its best not to kill a thread abruptly. The resources allocated by the C function may not be freed.

If the C function is waiting on something, you can trigger a condition to return back and check for the error code.

vikesh
  • 31
  • 4
-3

There is nothing special about the code being native "Called through JNI" or pure Java , All IO code use JNI code and you can stop it using ordinary Java Thread methods.

Just Use Thread.Stop() or Thread.Inturrupt()

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
  • Please take a look at this tutorial http://geekexplains.blogspot.com/2008/07/alternatives-of-stop-suspend-resume-of.html – Mr.Me Feb 07 '13 at 10:11
  • 1
    You can't use Thread.stop() because it is deprecated, and you can't use Thread.interrupt() because it doesn't have any effect on JNI code. The JVM's I/O code uses *Thread.interrupt()-aware* JNI code, not just 'JNI code'. I have no idea what 'called throw JNI' is supposed to mean. Your reference is non-normative and non-authoritative. Not an answer. – user207421 Feb 07 '13 at 10:37
  • From what I understand is that the Native code will eventually run in the same thread that was lunched by the JVM so doing anything to that thread will affect the JNI code used in it. The question didn't ask about the memory allocation so I didn't consider it when answered the question – Mr.Me Feb 07 '13 at 10:56
  • @Mr.Me Interrupt is also not worked in my case because there should be a while loop which checks continues thread status in inturrupted or not? How can i check in my case, there is no while loop – sam_k Feb 07 '13 at 11:01
  • You don't need a while loop to handle the interrupt code. Interrupt will raise InturruptException and your thread will get killed if that exception was not handled – Mr.Me Feb 07 '13 at 11:09
  • look here http://stackoverflow.com/questions/6378483/thread-interrupt-vs-jni-function-call – Mr.Me Feb 07 '13 at 11:11
  • @Mr.Me Check Accepted answer its have while loop – sam_k Feb 08 '13 at 08:17
  • please at least put the structure of your c++ code so we can find a solution. because inturrupt doesn't have any effect – Mr.Me Feb 08 '13 at 16:00