android jni 多线程编程 join thread:

//// Created by 11 on 2019/3/7.//#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;class One{public:    void func();};extern "C" {jstring Java_com_reactnative_jason_jnidome_JNIClass_getStringForJNI(JNIEnv *env, jobject jobject1, jstring jstring1) {    const char *outs = env->GetStringUTFChars(jstring1, JNI_FALSE);    One one;    one.func();    return env->NewStringUTF(outs);}}#define NUM_THREADS     5struct Augments{    int arg;};void outString(const char *ins){    LOGI("jason:%s", ins);}void outInt(int a){    LOGI("jason:%d", a);}void outFloat(float a){    LOGI("jason:%f", a);}void *PrintHello(void *threadid){    Augments *augments = (Augments *)(threadid);    sleep((unsigned int)(augments->arg+1));    outInt(augments->arg);    pthread_exit(NULL);}void One::func() {    pthread_t pThread[NUM_THREADS];    Augments augments[NUM_THREADS];    pthread_attr_t attr;    void *status;    //创建可连接线程    pthread_attr_init(&attr);    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);    for (int i = 0; i < NUM_THREADS; ++i) {        outString("create thread id:");        outInt(i);        augments[i].arg=i;        int ret = pthread_create(&pThread[i], NULL, PrintHello, (void*)&(augments[i]));        if (ret!=0){            outString("create thread error:");            outInt(i);        }    }    pthread_attr_destroy(&attr);    outString("create end");    //等待每个线程执行完毕    for (int i = 0; i < NUM_THREADS; ++i) {        int ret=pthread_join(pThread[i], &status);        if (ret){            outString("wait join error:");            outString((const char *)(status));            outInt(i);        }    }    outString("end---");}

更多相关文章

  1. Android Activity的setTitle,AlertDialog,Toast操作是否都必须在
  2. Android的进程和线程
  3. 简单解释Android中的任务、进程和线程
  4. Android Handler机制详解:在线程中新建Handler
  5. [转] 有关Android线程的学习
  6. android 在android中教你一行代码判断是不是主线程
  7. 《Android开发艺术探索》第十章Android的消息机制+第十一章Andro
  8. 《Android Dev Guide》系列教程5:Android进程和线程

随机推荐

  1. sql获取存储过程返回数据过程解析
  2. MySQL中or、in、union与索引优化详析
  3. win10 mysql导出csv的两种方式
  4. MySQL中索引失效的常见场景与规避方法
  5. mysql存储过程之循环语句(WHILE,REPEAT和LO
  6. mysql事件之修改事件(ALTER EVENT)、禁用事
  7. mysql的计划任务与事件调度实例分析
  8. mysql触发器之触发器的增删改查操作示例
  9. mysql日期处理函数实例解析
  10. Mysql临时表原理及创建方法解析