I have a an AsyncTask in Android 2.3.3 with SDK 2.2.
Within the 开发者_运维技巧task/class I am utilizing a C-library with the NDK.
Instead of having a while(condition)
loop in Java, I have this loop in C to avoid expensive calls via JNI during each loop iteration.
If I had the while(condition)
loop in Java I would call Thread.yield()
at the end of each loop-iteration to allow the Thread to get suspended.
Is there a possibility in C to get the same behaviour?
I tried it with usleep(10)
at the the end of the while(condition)
loop in C but I did not get the expected behaviour.
Any suggestions?
If one really wants to call Thread.yield() from C-Code one could do the following:
Java_de_company_MyClass_setupJNI(JNIEnv* env, jobject thiz) {
(...snip...)
jclass threadClass = (*env)->FindClass(env, "java/lang/Thread");
jmethodID yieldFunctionID = (*env)->GetStaticMethodID(env, threadClass, "yield", "()V");
(*env)->CallStaticVoidMethod(env, threadClass, yieldFunctionID);
(...snip...)
}
Don't forget error checking.
You can use sched_yield()
(edited after Steven Bell's comment)
精彩评论