NDK 5 uses POSIX Threads

Source: Internet
Author: User

It is particularly simple to use threads in Java to implement the Runnable interface or inherit the Thread.

The thread used in NDK can directly call the java thread in the Activity, or start the thread through JNI, Which is originated from the thread library in POSIX.


Use pthread in POSIX

Header file <pthread. h>


If the Posix thread is started, it cannot interact with the Android Java layer, mainly because the POSIX thread cannot directly call JNIEnv.
Therefore, we need to associate native thread with JVM first to obtain JNIEnv. Without JNIEnv, you cannot call back the JAVA layer method.


Write an Activity and print a statement Toast.


Public class MainActivity extends Activity {static {System. loadLibrary ("ndk-thread");} public static MainActivity instance; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. start ). setOnClickListener (new OnClickListener () {public void onClick (View arg0) {JNI. startThreadJNI (); // JNI Native method, start thread}); instance = this;} public void showMsg (final String msg) {Log. d ("showMsg", "msg:" + msg); MainActivity. instance. runOnUiThread (new Runnable () {@ Overridepublic void run () {Toast. makeText (instance, msg, Toast. LENGTH_SHORT ). show (); // callback pop-up message }});}}


JNI. java

public class JNI { public JNI() { } public native static void startThreadJNI();}

Create a Callback. java showMsg Method for Callback Activity

public class Callback {public void setMsg(String msg) {MainActivity.instance.showMsg(msg);}public Callback() {}}


We can get the JVM object in the JNI_OnLoad method. This method is automatically executed by the framework. The JNI. h header file contains a prototype.

JavaVM * jvm1 = NULL; jobject obj1 = NULL; jmethodID mid1 = NULL; jint JNI_OnLoad (JavaVM * vm, void * reserved) {jvm1 = vm; return JNI_VERSION_1_4 ;} void * run_task (void * args) {// JNIEnv * env = NULL; int n = (* jvm1)-> AttachCurrentThread (jvm1, & env, NULL); // obtain JNIEnv if (n = 0) {jstring msg = (* env)-> NewStringUTF (env, "Yes Thread Running. "); (* env)-> CallVoidMethod (env, obj1, mid1, msg); // Callback method (* env)-> DeleteGlobalRef (env, obj1); // Delete reference (* jvm1)-> DetachCurrentThread (jvm1); // This must be called; otherwise, an error is reported, intended to cancel thread-jvm Association} LOGI ("44");} void init_instance (JNIEnv * env) {LOGI ("instance. "); jclass jz1 = (* env)-> FindClass (env," com/birds/android/ndk/thread1/Callback "); jmethodID mid = (* env) -> GetMethodID (env, jz1, "<init>", "() V"); jobject myobj = (* env)-> NewObject (env, jz1, mid ); obj1 = (* env)-> NewGlobalRef (env, myobj); LOGI ("OK Instance Done. "); mid1 = (* env)-> GetMethodID (env, jz1," setMsg "," (Ljava/lang/String;) V ");}






Compile the JNI layer code. Write C Code directly

JNIEXPORT void JNICALL initialize (JNIEnv * env, jclass jclas) {init_instance (env); // mainly prepares some Object Instantiation, and Callback instantiates pthread_t thread1; int n = pthread_create (& thread1, NULL, run_task, NULL); // start the thread and call the run_task method if (n! = 0) {// thread creation failure jclass exceptionClazz = (* env)-> FindClass (env, "java/lang/RuntimeException"); (* env) -> ThrowNew (env, exceptionClazz, "create thread error. ");}}











Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.