What are the consequences if we try to attach a Native Thread permanently to the DVM (JVM)?

205 views
Skip to first unread message

SSuman185

unread,
Feb 21, 2012, 2:24:31 AM2/21/12
to android-ndk
Is it feasible to attach a native thread permanently to the JVM
(AttachCurrentThread) (or) is it better to attach when ever required
(calling java functions) and detach immediately once the work is done

I wrote a sample native app with the above cases, didn't find any
difference. But by googling, vaguely I came to know that, when
attached to JVM , JVMs thread scheduling is responsible for scheduling
else OS will schedule the native thread (if not attached). Is this
true?

It is important to detach any thread that has been previously
attached; otherwise, the program will not exit when you call
DestroyJavaVM. - http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniref.html#attach

Will there by any performance issues?
Please let me know if anyone knows, its one my important design
aspect.

Thanks & Regards,
SSuman185

Elliott Hughes

unread,
Feb 21, 2012, 1:51:54 PM2/21/12
to android-ndk


On Feb 20, 11:24 pm, SSuman185 <aluvala.su...@gmail.com> wrote:
> Is it feasible to attach a native thread permanently to the JVM
> (AttachCurrentThread) (or) is it better to attach when ever required
> (calling java functions) and detach immediately once the work is done

the former. attaching and detaching are relatively heavyweight
operations (with the exception of attaching an already-attached
thread).

> I wrote a sample native app with the above cases, didn't find any
> difference. But by googling, vaguely I came to know that, when
> attached to JVM , JVMs thread scheduling is responsible for scheduling
> else OS will schedule the native thread (if not attached). Is this
> true?

no.

> It is important to detach any thread that has been previously
> attached; otherwise, the program will not exit when you call
> DestroyJavaVM. -http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jnir...

Aluvala Suman

unread,
Feb 27, 2012, 1:15:58 AM2/27/12
to andro...@googlegroups.com
Hi Elliott,

Thanks for the reply.
So, do you suggest to attach the native thread permanently to the VM?
Will there be any side effects if i do so?

Thanks & Regards,
SSuman185

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


Elliott Hughes

unread,
Feb 27, 2012, 12:36:34 PM2/27/12
to android-ndk
you'll get warnings logged if those threads exit without detaching,
but it's only a warning at the moment, and unless we come up with a
really good reason why we _need_ to be more strict, i don't see us
getting more strict there because it isn't something that people using
third-party native libraries necessarily have much control over.

the best approach in your _own_ code is usually to start threads in
managed code (with "new Thread()" or even an Executor) and have their
"run" method make a native method call or calls and have all your
native code in regular native methods. let the VM worry about thread
management.
Reply all
Reply to author
Forward
0 new messages