473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

JNI and Java Threads

Rob
Hi there,

Firstly, apologies for x-post but I'm hoping the post is relevant to both
groups...

I have written a native implementation in C, and a java wrapper class. This
all works fine and correctly while I am calling the wrapper methods from a
single thread, however, my application requires calling the wrapper methods
for the native code from several different (java) threads.

And this is where I get stuck! - I am getting garbage values back from
native unless I use the same thread each time.

I have implemented something whereby I start a new thread, and then
wait/notify as each of the other requesting threads contact this single
thread, and then that single threads toodles off to native, but that causes
problems with waiting for return values and also makes the code far more
complicated that I believe it should be.

I have seen mentions of the functions 'JNI_GetCreatedJavaVMs' and
'AttachCurrentThread()', but as far as I can see, these are mainly for
contacting the JVM from the C and not the other way around. (I may well be
(and hope I am!) wrong).

This is a passage from a book I have which seems to answer my question,
kinda:

"Since a native method may be called from different threads in a Java
application, each different invocation of that native method will have a
different JNIEnv pointer passed to it. Passing this information to another
thread will result in unpredictable behaviour.
On the other hand, for a given method, from a given thread, the JVM
guarantees that the same JNIEnv pointer is passed on each call to that
method."

This seems to say that what I want is not possible, but regardless its what
I need lol so one way or another I need to figure it out. (I'm calling
native methods from the GUI and from event register calls etc.) Can anyone
offer any relatively clean work arounds or suggestions to how this can be
done? (other than the messy way I've tested as suggested above?)

Thanks for any anyone can offer, I've been on this for days!!! I'm hoping
I've been way off base and that the answer is simple.... but hey, I also
understand that this is computing...

Rob.
Jul 17 '05 #1
5 9204
Rob wrote:
And this is where I get stuck! - I am getting garbage values back from
native unless I use the same thread each time.
[SNIP]
This seems to say that what I want is not possible, but regardless its what
I need lol so one way or another I need to figure it out. (I'm calling
native methods from the GUI and from event register calls etc.) Can anyone
offer any relatively clean work arounds or suggestions to how this can be
done? (other than the messy way I've tested as suggested above?)

Ok. Well you've told us a little on *how* you think you need to do what
you want to do, but not *what* you need to do.

Tell us *what* you want, then we can help more.
Without further details, I'd guess that you're passing around and
holding transient pointers when you shouldn't. By going to a single
thread, you hide this problem by not triggering the symptom, but it
probably is still there. I'd wager that you have some broken code on the
C side that needs cleaning up.

Jul 17 '05 #2
Rob
"Jon A. Cruz" <jo*@joncruz.org> wrote in message
news:40**************@joncruz.org...

Ok. Well you've told us a little on *how* you think you need to do what
you want to do, but not *what* you need to do.

Tell us *what* you want, then we can help more.
Without further details, I'd guess that you're passing around and
holding transient pointers when you shouldn't. By going to a single
thread, you hide this problem by not triggering the symptom, but it
probably is still there. I'd wager that you have some broken code on the
C side that needs cleaning up.


Hmmm, well, the application is to control an audio cd. The native side has
various methods (open, play, getCurrentTime, stop etc.), and the java side
has a wrapper class which holds the device handle for the cd drive. So when
I call the native 'open', a device handle (int) is returned to the java
which I then use as an argument to call the native methods play, stop etc.

So from the GUI, I am pressing the 'open' then the 'play' button, but from
another thread I am polling the wrapper classes 'getCurrentTime' and other
methods to continually update a textual display of the cd status. And in
future I'll probably be wanting to add more thready methods for more
advanced status features.

If I add a 'button' to the GUI, which calls the 'getCurrentTime' method, it
works fine... however, if I press the 'open' button on the GUI, and then
rely on a separate thread to poll native and update the time/clock display
on the cd player, I get garbage values back...

I'm not entirely sure which info is needed, but is this any help as to what
I am doing wrong?

Thanks,
Rob.

Jul 17 '05 #3
Rob...

I've found JNI to be very buggy when called from seperate threads.....

....So make sure only one thread ever calls your native code!

Make your wrapper methods, such as playCD() notify a seperate thread which
in turn calls the native code.
Something like this:

int methodToCall;
final int PLAY = 1;

//run as seperate thread using thread.start()
public synchronized void run()
{
while( true )
{
wait();
switch( methodToCall )
{
case PLAY:
JNI_PlayCD(JNIEnv env, ...)
break;
.....
}
}
}

public synchronized void playCD()
{
methodToCall = PLAY;
notifyAll();
}
"Rob" <.@.com> wrote in message
news:40**********************@news.dial.pipex.com. ..
Hi there,

Firstly, apologies for x-post but I'm hoping the post is relevant to both
groups...

I have written a native implementation in C, and a java wrapper class. This all works fine and correctly while I am calling the wrapper methods from a
single thread, however, my application requires calling the wrapper methods for the native code from several different (java) threads.

And this is where I get stuck! - I am getting garbage values back from
native unless I use the same thread each time.

I have implemented something whereby I start a new thread, and then
wait/notify as each of the other requesting threads contact this single
thread, and then that single threads toodles off to native, but that causes problems with waiting for return values and also makes the code far more
complicated that I believe it should be.

I have seen mentions of the functions 'JNI_GetCreatedJavaVMs' and
'AttachCurrentThread()', but as far as I can see, these are mainly for
contacting the JVM from the C and not the other way around. (I may well be (and hope I am!) wrong).

This is a passage from a book I have which seems to answer my question,
kinda:

"Since a native method may be called from different threads in a Java
application, each different invocation of that native method will have a
different JNIEnv pointer passed to it. Passing this information to another thread will result in unpredictable behaviour.
On the other hand, for a given method, from a given thread, the JVM
guarantees that the same JNIEnv pointer is passed on each call to that
method."

This seems to say that what I want is not possible, but regardless its what I need lol so one way or another I need to figure it out. (I'm calling
native methods from the GUI and from event register calls etc.) Can anyone offer any relatively clean work arounds or suggestions to how this can be
done? (other than the messy way I've tested as suggested above?)

Thanks for any anyone can offer, I've been on this for days!!! I'm hoping
I've been way off base and that the answer is simple.... but hey, I also
understand that this is computing...

Rob.

Jul 17 '05 #4
nos

"Rob" <.@.com> wrote in message
news:40**********************@news.dial.pipex.com. ..
Hi there,

Firstly, apologies for x-post but I'm hoping the post is relevant to both
groups...

I have written a native implementation in C, and a java wrapper class. This all works fine and correctly while I am calling the wrapper methods from a
single thread, however, my application requires calling the wrapper methods for the native code from several different (java) threads.

And this is where I get stuck! - I am getting garbage values back from
native unless I use the same thread each time.

I have implemented something whereby I start a new thread, and then
wait/notify as each of the other requesting threads contact this single
thread, and then that single threads toodles off to native, but that causes problems with waiting for return values and also makes the code far more
complicated that I believe it should be.

I have seen mentions of the functions 'JNI_GetCreatedJavaVMs' and
'AttachCurrentThread()', but as far as I can see, these are mainly for
contacting the JVM from the C and not the other way around. (I may well be (and hope I am!) wrong).

This is a passage from a book I have which seems to answer my question,
kinda:

"Since a native method may be called from different threads in a Java
application, each different invocation of that native method will have a
different JNIEnv pointer passed to it. Passing this information to another thread will result in unpredictable behaviour.
On the other hand, for a given method, from a given thread, the JVM
guarantees that the same JNIEnv pointer is passed on each call to that
method."

This seems to say that what I want is not possible, but regardless its what I need lol so one way or another I need to figure it out. (I'm calling
native methods from the GUI and from event register calls etc.) Can anyone offer any relatively clean work arounds or suggestions to how this can be
done? (other than the messy way I've tested as suggested above?)

Thanks for any anyone can offer, I've been on this for days!!! I'm hoping
I've been way off base and that the answer is simple.... but hey, I also
understand that this is computing...

Rob.

Does the 'c' program have to be re-entrant?
Jul 17 '05 #5
[unofficial newsgroup trimmed]

Rob wrote:

So from the GUI, I am pressing the 'open' then the 'play' button, but from
another thread I am polling the wrapper classes 'getCurrentTime' and other
methods to continually update a textual display of the cd status. And in
future I'll probably be wanting to add more thready methods for more
advanced status features.
Hmmm.. that there sounds fairly poor as far as design goes. Instead of
polling, you should register listeners and have the native code generate
notifications. In general, polling should be avoided when possible.


If I add a 'button' to the GUI, which calls the 'getCurrentTime' method, it
works fine... however, if I press the 'open' button on the GUI, and then
rely on a separate thread to poll native and update the time/clock display
on the cd player, I get garbage values back...


Hmmm... what kind of 'garbage', and what should you get? Have you added
debug output to your native code to see what it's getting and doing?

Are you storing anything in the JNI side of things? Are you keeping any
object references, jenv pointers or anything else?

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: mthlv12 | last post by:
In C you can create a TRUE daemon which you start by executing it and it severs itself from the terminal you started it from. In java you can use javaw in windows, but in unix you would have to...
47
by: Theatre Mgmt | last post by:
Sun project seeks to solve Java memory, execution issues http://story.news.yahoo.com/news?tmpl=story&u=/infoworld/20050325/tc_infoworld/58059&e=1&ncid= San Francisco (InfoWorld) - Sun...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
1
by: jopeter | last post by:
hi i have wrote this progaram in java. it finf the palidrome numbers and print it. its by threads it has some errors and i can t run it. please help me. package palindromen; import...
63
by: s0suk3 | last post by:
I've been programming Python for a couple of years now. Now I'm looking to move on to either C++ or Java, but I'm not sure which. Which one do you think will be a better transition for a Python...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.