473,387 Members | 1,592 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,387 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 9214
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.