jane
jane

Reputation: 3

java thread blocked when calling a time-consuming native method

I have a thread that will call a native method. And I don't know why when the thread calling native method,it will always stop and wait for native method to complete. After the native method completed ,the thread will then keep running. The problem is that the native method is so time-consuming. But I want my java thread keep on running even though the native method is still running.I don't want my java thread stop and wait.

The native method is a jni function which will call c files generated by matlab coder.The code takes only 1 seconds when running in matlab. But it takes 15 seconds when running in android.


A brief demonstration to my project:

in java:

 new Thread(new Runnable() {
            @Override
            public void run() {
            //record and get audio samples in real-time as an input to native method
            //call native method
            //print the result from native method
       }).start();

in native method:

JNIEXPORT jobjectArray JNICALL
Java_com_example_user_MyProject_MainActivity_getpitch(JNIEnv *env, jobject instance,jdoubleArray array_) {

//call c function generated by matlab (the input is the array we get from java)
//return the results calculated by the c function to java

}

My problem is ,is there any solution if I want my java thread keep on running even though the native method hasn't finished.(Because I need to get audio samples in real-time.If thread stop, it will not get the audio samples. And then we will missed many samples.)

Sorry for my poor English again..It's the first time I ever post a question.

Upvotes: 0

Views: 892

Answers (3)

Puneet
Puneet

Reputation: 113

You can make the call to native code as async using Callable which is runnable but async and then can use the Future Object to query the results.

public class TestCallable implements Callable {

@Override
public String call() throws Exception {

    //return the thread name executing this callable task
    return yourNativeCall();
}

]

Upvotes: 0

Solomon Slow
Solomon Slow

Reputation: 27125

I don't know why when the thread calling native method,it will always stop and wait for native method to complete.

Well, that's what happens when you call a Java method, right? If you write this;

foo(...);
bar(...);

Where foo() is a regular Java method, then you would not expect bar(...) to be entered before foo(...) returns, right?

So why should your expectation be any different if foo() happens to have a native implementation?

Consider this: Suppose you are the author of some pure Java graphics library, and suppose you decide to release a new, faster version with native implementations of some of the public methods. Who's going to want to port their code to use your new version if changing a method to native completely changes what happens when they call it?

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006869

why would my java thread stop and wait for native method to finish?

A thread executes code sequentially. It will not execute the code after the native call until the native call completes.

And is there any solution if I want my java thread keep on running even though the native method hasn't finished.

Then use two threads: one for the blocking calls to the native library, and one doing whatever else you want to have happening.

Upvotes: 3

Related Questions