I've found here https://stackoverflow.com/a/12901159/6655884 a function call where it attaches the thread with AttachCurrentThread
, does the call and then detaches it with DetachCurrentThread
.
I want to know if this process is costly. I have a C++ function
void sendEvent(Event event) {
//call java function here
}
that will be called by several C++ threads to send events to the Java side, so I cannot simply attach a thread and never detach it because many different threads are going to call sendEvent
. So I wanna know if doing AttachCurrentThread
, calling Java and then DetachCurrentThread
at every sendEvent
call is costly. If it is, what should I do instead?
Although you CAN attach the calling thread on a per-JNI-call basic, you really SHOULD NOT, unless you have no other choice. A native thread MUST attach itself to the JVM before it can make JNI calls. To avoid unnecessary overhead, the thread really should attach itself as soon as possible (at least before the 1st JNI call), and stay attached until it no longer needs to make any further JNI calls.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments