Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Global References, Concurrency and Instantiation in JNI

810144Nov 2 2010 — edited Nov 3 2010
Not completely sure I've got the right spot for this but here goes anyway, I have two questions that I'd like to find some help on.

Global References in the Java Native Interface - yeah we shouldn't use them, but I am anyway. My question is pretty simple, but I've had absolutely no luck finding any documentation which directly answers it.

Everywhere I look I can find the following:

+"You can use a global reference across multiple invocations of a _native method_. A global reference can be used across _multiple threads_ and remains valid until it is freed by the programmer."+

BUT - what about across instantiations of the objects that call the native program ???

For example:

class HelloWorld {
public native void print();
static {
System.loadLibrary("HelloWorld");
}
}

then elsehwere I instantiate:

w1 = new HelloWorld() <-Thread 0
w1.print(); <-Thread 0

and elswhere:

w2 = new HelloWorld() <-Thread 1
w2.print(); <-Thread 1

My question is can global references from w1 native function interfere/incurr reuse with global references in w2?

My native code is in C and I've declared the variables containing the global references in C globally as static (i.e. static declaration for said variables occurs outside of the C function)

I did this to achieve the following:
1) Each instantiation-thread (and its subordinate native function calls) can repeatedly be using the same set of global references, but
2) those global references would not be reused (or conflict with each other) between threads - i.e. each instantiation-thread has its own set of identical global reference scope under the instantiation thread.

I'm trying to achieve multiple sets of repeated native calls where each set of repeated native invocations can repeatedly refer to its own set of global references.

If someone can please affirm or deny that this will achieve my goals (1 and 2 above) and provide insight on how to achieve them if not - it would be greatly appreciated.

And the second question is more of a JNI/C question - will globally declared variables (not global references and not static variables) be reused from invocation to invocation of the native function as well? I wouldn't expect these global variables to retain their values once an executable returns but I don't have much experience with performing repeated C library function calls from another language (i.e. Java)

thanks,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 1 2010
Added on Nov 2 2010
4 comments
525 views