Do I need JNI to disconnect an attached topic?

I have my own thread that needs to be called in Java. To do this, I need to bind the thread to the VM using AttachCurrentThread . Since this callback will occur quite frequently, the thread should probably remain attached. Invoking AttachCurrentThread several times is ok ("Attempting to attach a thread that is already connected is a non-statement.")

Should I call DetachCurrentThread before the thread exits, will it happen automatically or is it not even required? What happens if I call my resignation, but right? Is it just a leak, or can it damage the state of the virtual machine?

I checked the Java Native Interface specification, but either skipped this or not really specified.

My question relates specifically to Sun JDK 6 on Windows XP.

+6
java multithreading windows jvm jni
source share
1 answer

I think the confirmation you want is here: http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/invocation.html#wp1060

A native thread connected to the virtual machine must call DetachCurrentThread () to disconnect before exiting.

And in the next section there is a rationale:

The VM waits until the current thread becomes the only non-daemon user thread before it actually unloads. User streams include both Java streams and their associated streams.

+8
source share

All Articles