https://developer.android.com/training/articles/perf-jni.html
Threads
All threads are Linux threads, scheduled by the kernel. They're usually started from managed code (using
Thread.start
), but they can also be created elsewhere and then attached to the JavaVM. For example, a thread started with pthread_create
can be attached with the JNI AttachCurrentThread
or AttachCurrentThreadAsDaemon
functions. Until a thread is attached, it has no JNIEnv, and cannot make JNI calls.
Attaching a natively-created thread causes a
java.lang.Thread
object to be constructed and added to the "main" ThreadGroup
, making it visible to the debugger. Calling AttachCurrentThread
on an already-attached thread is a no-op.
Android does not suspend threads executing native code. If garbage collection is in progress, or the debugger has issued a suspend request, Android will pause the thread the next time it makes a JNI call.
Threads attached through JNI must call
DetachCurrentThread
before they exit. If coding this directly is awkward, in Android 2.0 (Eclair) and higher you can use pthread_key_create
to define a destructor function that will be called before the thread exits, and call DetachCurrentThread
from there. (Use that key with pthread_setspecific
to store the JNIEnv in thread-local-storage; that way it'll be passed into your destructor as the argument.)
No comments:
Post a Comment