java调用耗时的native方法时线程阻塞
java thread blocked when calling a time-consuming native method
我有一个将调用本机方法的线程。而且不知道为什么线程在调用native method的时候,总是会停下来等待native method完成。本地方法完成后,线程将保持运行。问题是原生方法太耗时了。但是我希望我的 java 线程继续 运行 即使本机方法仍然 running.I 不希望我的 java 线程停止并等待。
native方法是一个jni函数,会调用matlab生成的c文件coder.The代码在matlab中运行只需要1秒。但是在android.
中运行需要15秒
我的项目的简要演示:
在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();
在本机方法中:
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
}
我的问题是,如果我希望我的 java 线程继续 运行,即使本机方法尚未完成,有什么解决方案吗?(因为我需要获取真实的音频样本-time.If线程停止,它不会获取音频样本。然后我们会错过很多样本。)
再次为我糟糕的英语道歉..这是我第一次post提问。
why would my java thread stop and wait for native method to finish?
线程按顺序执行代码。在本机调用完成之前,它不会执行本机调用之后的代码。
And is there any solution if I want my java thread keep on running even though the native method hasn't finished.
然后使用两个线程:一个用于阻塞对本机库的调用,另一个执行您想要发生的任何其他事情。
I don't know why when the thread calling native method,it will always stop and wait for native method to complete.
好吧,这就是调用 Java 方法时发生的情况,对吧?如果你这样写;
foo(...);
bar(...);
其中 foo()
是一个常规的 Java 方法,那么您不会期望 bar(...)
在 foo(...)
returns 之前输入,对吗?
那么,如果 foo()
碰巧有本地实现,为什么您的期望会有所不同?
考虑一下:假设您是一些纯 Java 图形库的作者,并且假设您决定发布一个新的、更快的版本,其中包含一些 public 方法的本机实现。如果将方法更改为 native
会完全改变他们调用它时发生的情况,谁会想要移植他们的代码以使用您的新版本?
您可以使用可运行但异步的 Callable 以异步方式调用本机代码,然后可以使用 Future 对象查询结果。
public class TestCallable 实现 Callable {
@Override
public String call() throws Exception {
//return the thread name executing this callable task
return yourNativeCall();
}
]
我有一个将调用本机方法的线程。而且不知道为什么线程在调用native method的时候,总是会停下来等待native method完成。本地方法完成后,线程将保持运行。问题是原生方法太耗时了。但是我希望我的 java 线程继续 运行 即使本机方法仍然 running.I 不希望我的 java 线程停止并等待。
native方法是一个jni函数,会调用matlab生成的c文件coder.The代码在matlab中运行只需要1秒。但是在android.
中运行需要15秒我的项目的简要演示:
在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();
在本机方法中:
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
}
我的问题是,如果我希望我的 java 线程继续 运行,即使本机方法尚未完成,有什么解决方案吗?(因为我需要获取真实的音频样本-time.If线程停止,它不会获取音频样本。然后我们会错过很多样本。)
再次为我糟糕的英语道歉..这是我第一次post提问。
why would my java thread stop and wait for native method to finish?
线程按顺序执行代码。在本机调用完成之前,它不会执行本机调用之后的代码。
And is there any solution if I want my java thread keep on running even though the native method hasn't finished.
然后使用两个线程:一个用于阻塞对本机库的调用,另一个执行您想要发生的任何其他事情。
I don't know why when the thread calling native method,it will always stop and wait for native method to complete.
好吧,这就是调用 Java 方法时发生的情况,对吧?如果你这样写;
foo(...);
bar(...);
其中 foo()
是一个常规的 Java 方法,那么您不会期望 bar(...)
在 foo(...)
returns 之前输入,对吗?
那么,如果 foo()
碰巧有本地实现,为什么您的期望会有所不同?
考虑一下:假设您是一些纯 Java 图形库的作者,并且假设您决定发布一个新的、更快的版本,其中包含一些 public 方法的本机实现。如果将方法更改为 native
会完全改变他们调用它时发生的情况,谁会想要移植他们的代码以使用您的新版本?
您可以使用可运行但异步的 Callable 以异步方式调用本机代码,然后可以使用 Future 对象查询结果。
public class TestCallable 实现 Callable {
@Override
public String call() throws Exception {
//return the thread name executing this callable task
return yourNativeCall();
}
]