Package com.tigger; public class the MyThread { static {
// load library, ensure jvm at startup will load the System.loadLibrary ( "TiggerThreadNative" ); } public static void main (String [] args) { the MyThread myThread = new new the MyThread (); myThread.start0 (); } Private Native void START0 (); }
1, write java code MyThread.java a call using jni c procedures, uploaded to / at home / mythread / com / tigger / directory
2, compiled into class program javac MyThread.java
3, generates header javah packageName.ClassName this command to be executed outside the package, commands to be executed in the javac packet among
javah com.tigger.MyThread
4, will be compiled header file to the same directory under MyThread.java
5, start writing c program threadNew.c, and put / home / mythread / com / tigger / directory
#include <pthread.h> #include <stdio.h> #include "com_tigger_MyThread.h" // need .h files compiled just introduced pthread_t PID; void * thread_entity ( void * Arg) { the while (. 1 ) { usleep ( 100 ); printf ( "the I AM new new the Thread \ the n-" ); } } // this method name needs to be copied from the method name just compiled .h header file, here parameter-coded JNIEXPORT void JNICALL Java_com_tigger_MyThread_start0 (JNIEnv * EVN, jobject C1) { pthread_create ( & PID, NULL, thread_entity, NULL); the while(1){ usleep(100); printf("I am main\n"); } } int main(){ return 0; }
6, threadNew.c compiled into a dynamic link library, so that java code can be load into memory
gcc -I /usr/java/jdk1.8.0_221/include -I /usr/java/jdk1.8.0_221/include/linux threadNew.c -fPIC -shared -o libTiggerThreadNative.so -pthread
7, the compiled .so move a file to / home / mythread / directory, and add to the path, so in order to load the jvm
mv libTiggerThreadNative.so /home/mythread/
Enter mythread directory execute the command
export LD_LIBRARY_PATH=/home/mythread/
8, test
java com.tigger.MyThread