Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Java thread which call JNI function

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.

like image 861
sam_k Avatar asked Feb 07 '13 09:02

sam_k


People also ask

What JNI calls?

In software design, the Java Native Interface (JNI) is a foreign function interface programming framework that enables Java code running in a Java virtual machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages ...

How does Java JNI work?

The JNI is a native programming interface. It allows Java code that runs inside a Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages, such as C, C++, and assembly.

Is JNI fast?

When talking about JNI, there are two directions: java calling C++, and C++ calling java. Java calling C++ (or C) via the "native" keyword is very fast, around 50 clock cycles. However, C++ calling Java is somewhat slow.

What is a JNI wrapper?

JNIWrapper allows Java applications to interoperate with native applications and libraries written in different programming languages, such as C/C++, Pascal, ASM, etc. Cross-platform Java integration solutions which can be built on top of existing cross-platform native libraries.


Video Answer


2 Answers

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.

like image 38
vikesh Avatar answered Sep 27 '22 19:09

vikesh


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.

like image 113
Pavel Zdenek Avatar answered Sep 27 '22 17:09

Pavel Zdenek