创建线程
public static native void demoThread();
//
// Created by Administrator on 2020/3/3 0003.
//
#include "log.h"
#include <jni.h>
#include <string>
#include <pthread.h>
void *doInThread(void *) {
/*子线程中*/
LOGD("hello world");
return nullptr;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_demoThread(JNIEnv *env, jclass clazz) {
pthread_t handless;
int result = pthread_create(&handless, NULL, doInThread, NULL); /*返回0则表示创建成功*/
if (result == 0) {
LOGD("thread create success");
} else {
LOGD("thread create fail");
}
}
向子线程传递参数
struct ThreadArgs {
int id;
int num;
ThreadArgs(int id, int num) : id(id), num(num) {}
};
void *doInThread(void *args) {
/*子线程中*/
ThreadArgs *threadArgs = static_cast<ThreadArgs *>(args);
LOGD("ThreadArgs %d %d", threadArgs->id, threadArgs->num);
return nullptr;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_demoThread(JNIEnv *env, jclass clazz) {
pthread_t handless;
ThreadArgs *threadArgs = new ThreadArgs(1, 2);
int result = pthread_create(&handless, NULL, doInThread, threadArgs); /*返回0则表示创建成功*/
LOGD("thread create %s", result == 0 ? "success" : "fail");
}
获取线程的返回值
//
// Created by Administrator on 2020/3/3 0003.
//
#include "log.h"
#include <jni.h>
#include <string>
#include <pthread.h>
#include <unistd.h>
void *doInThread(void *args) {
/*子线程中*/
struct timeval begin;
gettimeofday(&begin, nullptr);
sleep(3); /*线程停止3s*/
struct timeval end;
gettimeofday(&end, nullptr);
LOGD("thread inner time %d ", end.tv_sec - begin.tv_sec);
return reinterpret_cast<void *>(1);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_demoThread(JNIEnv *env, jclass clazz) {
pthread_t handless;
int result = pthread_create(&handless, NULL, doInThread, nullptr); /*返回0则表示创建成功*/
LOGD("thread create %s", result == 0 ? "success" : "fail");
void *ret = nullptr;
pthread_join(handless, &ret); /*等handless结束 ret线程的返回*/
LOGD("thread result %d", ret);
}
输出结果
2020-03-04 16:32:58.772 23566-23566/0 D/native-jni: thread create success
2020-03-04 16:33:01.774 23566-23782/0 D/native-jni: thread inner time 3
2020-03-04 16:33:01.775 23566-23566/0 D/native-jni: thread result 1
线程同步的问题
#include "log.h"
#include <jni.h>
#include <string>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex; /*互斥变量*/
pthread_cond_t cond; /*条件变量*/
pthread_t waitHandle;
pthread_t notifyHandle;
int flag = 0;
void *waitThread(void *args) {
LOGD("waitThread 1 ...");
pthread_mutex_lock(&mutex);
while (flag == 0) {
LOGD("waitThread wait ...");
pthread_cond_wait(&cond, &mutex); /*会释放mutex锁放入阻塞序列 state:1*/
}
pthread_mutex_unlock(&mutex);
LOGD("waitThread 2 ...");
return NULL;
}
void *notifyThread(void *args) {
LOGI("notifyThread 1 ...");
pthread_mutex_lock(&mutex); /*可保证顺序 避免waitThread中的 while在flag为1后调用*/
flag = 1;
pthread_mutex_unlock(&mutex);
LOGI("notifyThread 2 ...");
pthread_cond_signal(&cond); /*通知cond state:1执行*/
LOGI("notifyThread 3 ...");
return NULL;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_syncThread1(JNIEnv *env, jclass clazz) {
// TODO: implement syncThread1()
pthread_mutex_init(&mutex, nullptr);
pthread_cond_init(&cond, nullptr);
pthread_create(&waitHandle, NULL, waitThread, NULL);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_syncThread2(JNIEnv *env, jclass clazz) {
// TODO: implement syncThread2()
pthread_create(¬ifyHandle, NULL, notifyThread, NULL);
}