JNI Native Threads
Hey everybody!
In this post I will show you how to use native threads with Java. If you follow the previous posts you may have noted that each native method comes with a JNIEnv pointer and that pointer is used to access JNI’s vtable. The JNIEnv attaches the native call to the Java’s thread. The problem calling Java function from native threads is that you don’t have a JNIEnv to use, so how to proceed?
First you need a pointer to the JVM, of the JavaVM *
.
With this pointer in hands you can call AttachCurrentThread
to initalize a JNIEnv
variable. Okay, but now, how
to obtain the JavaVM *
pointer.
There are two functions that you can implement and that
are called each time that the JVM is load or unload. They are
JNI_OnLoad
and JNI_OnUnload
respectively. The last one
will not be used at this example.
So now we have a full picture.
- Implement
JNI_OnLoad
. - Save
JavaVM *
pointer. - Call
AttachCurrentThread
to initialized oneJNIEnv **
. - Call any JNI function using that
JNIEnv **
pointer. - Call
DetachCurrentThread
to detach the previously attached thread.
The details can be grasped from the example code above. The comments provides further guidance. Take a look:
Cheers :+1: