28장. 네이티브 쓰레드 실행하기
네이티브 레이어에서 c++11 에 추가된 std::thread를 실행해 보자.
$ vi StartThread.cpp#include <jni.h>
#include <iostream>
#include <thread>
void startThreads
(
JNIEnv *env,
jobject thiz,
jint count
)
{
//start thread1
std::thread th1 = std::thread([&] {
for(int i=1; i<=count; ++i) {
std::cout << "[thread1] counting : " << i << std::endl;
}
});
//start thread2
std::thread th2 = std::thread([&] {
for(int i=count; i>0; --i) {
std::cout << "[thread2] counting : " << i << std::endl;
}
});
//waiting for finish of the thread1
if(th1.joinable()) {
th1.join();
}
//waiting for finish of the thread2
if(th2.joinable()) {
th2.join();
}
}
JNIEXPORT jint JNICALL JNI_OnLoad
(
JavaVM *vm,
void *reserved
)
{
JNIEnv *env;
if(vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6)) {
return -1;
}
JNINativeMethod nm[1] = {
{
const_cast<char*>("startThreads"),
const_cast<char*>("(I)V"),
reinterpret_cast<void*>(startThreads)
}
};
jclass cls = env->FindClass("Client");
env->RegisterNatives(cls, nm, 1);
return JNI_VERSION_1_6;
}쓰레드 두 개를 만들어 수를 카운팅한다.
코드를 컴파일해 라이브러리로 만든다.
자바 코드에서 라이브러리를 사용해 보자.
컴파일하고 실행해 본다.
Last updated
Was this helpful?