Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7179057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:03:48+00:00 2026-05-28T17:03:48+00:00

I find myself needing to call back into the JVM from arbitrary native threads

  • 0

I find myself needing to call back into the JVM from arbitrary native
threads that may or may not be long-lived. It’s easy enough call
AttachCurrentThread prior to every Java method invocation on the
assumption that it’s a (presumably fast) no op if the thread’s already
attached, but I find myself wondering what the JVM does with attached
native threads when they go away somewhere in native-land without
being officially detached. Or in other words, is this going to be a
problem? If so, then I find myself wondering about the overhead of
attaching/detaching for each Java method invocation. Ideally each
thread would just register itself upon startup and detach on shutdown
but again, I’m writing code that must be callable from arbitrary
threads running code that has no idea it’s running inside of Android,
so that’s not a realistic option in this case.

Any thoughts on the matter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:03:49+00:00Added an answer on May 28, 2026 at 5:03 pm

    I find myself wondering what the JVM does with attached native threads when they go away somewhere in native-land without being officially detached.

    The regular VM shuts down when all non-daemon threads have exited. Attaching a thread without detaching means that the VM cannot exit and has to wait for your thread to detach first. This is just a general case of VM, though. In Android it shouldn’t be a problem, as apps don’t really “exit”. Source.

    But detaching threads is highly recommended for proper memory management (see an official recommendation).

    I’m writing code that must be callable from arbitrary threads running code that has no idea it’s running inside of Android, so that’s not a realistic option in this case.

    Actually, the implementation of a self-detaching thread is quite easy. Pass “destructor” reference to pthread_key_create(destKey_, threadDestructor); and inside this “destructor” call JNI method cached_jvm->DetachCurrentThread();.

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every few months I find myself needing to call Win32 from C#. Though I've
I often find myself needing reference to an object that is several objects away,
I find myself needing to return various JSON results to the client from my
Sometimes I find myself needing to do something that could be automated or could
I often find myself needing to write functions to load/save from/to ASCII (or similar)
For development I find myself needing to copy table information from one table to
I find myself needing to log into various servers, set environment variables, and then
I often find myself needing a tool that would allow me to: search for
Is there an alternative to InAppSettingsKit that is simpler? I find myself needing only
I find myself needing to store public key certificates, and a single private key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.