Is calling AttachCurrentThread on JNI costly?

Guerlando OCs

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?

Remy Lebeau

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling JNI function to create object

From Java

Calling into a saved java object via JNI from a different thread

From Java

Calling a DLL from an Applet via JNI

From Java

JNI and Java: ant calling make or make calling ant?

From Java

JNI calling JVM functions/events from native code?

From Java

Are Cassandra heartbeat requests costly?

From Android

Can I use std::unique_lock to AttachCurrentThread and DetachCurrentThread from JNI?

From

How costly is .NET reflection?

From Dev

Android JNI - Call AttachCurrentThread without DetachCurrentThread

From Dev

What is the lifetime of JNI localrefs for C++ calling Java?

From Dev

Calling a static void Java method from JNI

From Dev

is Elasticsearch is free or costly?

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

appending values each time when calling JNI method

From Dev

How costly is casting in Kotlin?

From Dev

JVM crashes when calling JNI function during gc

From Dev

Calling dll implemented JNI from C++

From Dev

JNI Calling Java Method With Array Parameter

From Dev

JNI calling Java from C++ with multiple threads

From Dev

Calling C system calls from JNI

From Dev

JNI C++ UnsatisfiedLinkError When Calling Method

From Dev

Calling Java Methods from JNI results in program crash

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

Calling RenderScript from C / JNI

From Dev

Costly Immutable Operation

From Dev

Data type conversion when (JNI) calling a function from cpp lib

From Dev

Calling JNI C function in another source file

From Dev

Calling a Java variadic function from C through the JNI

From Dev

Backing up is costly in flex?

Related Related

HotTag

Archive