JNI & Multiple threads

I have been working with JNI on the EPOC|Symbian platform. In an initial version of my application I was able to implement a C++ application which invoked a Java class, this java class then called native methods which via the use of a symbain specific mixin dll interface can gain access back into the C++ application. In this first instance the Java class called the native methods form the main thread of the Java class and my application worked correctly.

In my current version of the application, my java class uses multiple threads, and it calls the native methods from any threads other than the main thread. In this scenario my java class fails to link back into the C++ application.

Is this a standard problem when using JNI, if it is any help or advice would be appreciated. If it appears to be Symbian specific than I will investigate further in that direction.

Thanks again

Mike

[927 byte] By [mike_hammett] at [2007-9-26 1:28:52]
# 1

I haven't encountered exactly your situation, but let me describe some issues we found with JNI and threads.

We do extensive call-in from C++ to java via JNI. What we have found is that we have to be very careful that java objects created in one C++ thread are only referenced from that thread. In other words, referencing the java objects from another C++ thread caused system failures.

bschauwe at 2007-6-29 1:15:14 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

I know this is a very old Thread but I have exactly the same Problem.

All Calls to JNI-functions from other Java-Threads than the Main-Thread (Threads, ActionListener, ...) cause a Kern-exec 3.

@bschauwe its not about the C++ Threads or Calling Java from C++ via JNI - in my case.

I also use Symbian (SonyEricsson P900) to develop a dll.

Has anybody a solution for this, or can explain why this erro occurs?

Thanks

Markus

mgsnova at 2007-6-29 1:15:14 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

I cannot get what you exactly mean. I guess the problem with threads may be only in JNI code. These are

- the native thread was not attached to JVM;

- you try to use a local reference in JNI, which was created (or got) in one thread but used in other one. In this case the main rule is before using an object reference in different threads convert it to a global reference. But keep in your mind that the global reference should be released explicitly as it is not garbage collected.

vitallis at 2007-6-29 1:15:14 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Yes, this is a fundamental restriction of JNI. Only the thead that is called from Java can communicate back to Java. If you want your other threads to "talk" to Java, your main thread will need to act as a proxy service.
salevin at 2007-6-29 1:15:14 > top of Java-index,Java HotSpot Virtual Machine,Specifications...