Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI thread model?

When I call a C/C++ from Java, is a new thread created by JavaVM or JNI to run the C/C++ code while my Java thread is waiting? I ask this because my C/C++ code runs something on the GPU and I need to check a specific buffer to get the result back. Once I have the result, I need to call my Java function again.

So I was thinking of creating a thread on the C++ side that continuously checks the buffer and once there is some data available, makes a call back to the Java side.

like image 751
Hossein Avatar asked Jul 14 '16 15:07

Hossein


People also ask

What is JNI and how it works?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is JNA vs JNI?

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.

Where is JNI used?

Programmers use the JNI to write Java native methods to handle those situations when an application cannot be written entirely in Java. The following examples illustrate when you need to use Java native methods: The standard Java class library does not support the platform-dependent features needed by the application.

What is Jclass in JNI?

typedef jobject jclass; In C++, JNI introduces a set of dummy classes to enforce the subtyping relationship. For example: class _jobject {}; class _jclass : public _jobject {}; ...


1 Answers

The JNI does not create any new thread behind the scene. A native function is executed in the same thread as the java method that calls the native function. And vice versa when native code calls a java method then the the java method is executed in the same thread as the native code calling the method.

It has consequence - a native function call returns to java code when the native function returns and native code continues execution when a called java method returns.

When a native code does a processing that should run in a separate thread the the thread must be explicitly created. You can either create a new java thread and call a native method from this dedicated thread. Or you can create a new native thread in the native code, start it and return from the native function.

// Call a native function in a dedicated java thread
native void cFunction();
...
new Thread() {
    public void run() {
        cFunction();
    }
};

// Create a native thread - java part
native void cFunction()
...
cFunction();

//  Create a native thread - C part
void *processing_function(void *p);
JNIEXPORT void JNICALL Java____cFunction(JNIEnv *e, jobject obj) {
    pthread_t t;
    pthread_create(&t, NULL, processing_function, NULL);    
}

If you use the second variant and you want to call a java callback from a thread created natively you have to attach the thread to JVM. How to do it? See the JNI Attach/Detach thread memory management ...

like image 82
Zaboj Campula Avatar answered Sep 24 '22 06:09

Zaboj Campula