-
lazy_static! {
pub(crate) static ref RUNTIME: Runtime = Runtime::new().unwrap();
}
#[no_mangle]
pub extern "system" fn Java_org_tikv_tijkv_RawKVClient_connect0(
env: JNIEnv, _class: JClass, pd_address: JString, client: JObject, f: JObject
) -> () {
println!("0");
let x = async move {
let pd_address: String = env.get_string(pd_address).unwrap().into();
println!("1 - {}", &pd_address);
tikv_client::RawClient::new(vec![pd_address])
.await
.map(|c| {
println!("2");
env.set_rust_field(client, "nativeHandle", c).unwrap();
env.call_method(f, "complete", "(Ljava/lang/Object;)Z", &[JValue::Object(client)]).unwrap();
println!("3");
})
};
RUNTIME.spawn(x);
} this code snippet fails to compile because
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Objects of both of these types can only be used in the thread where they were created. They can't be reused in any other threads. I highly recommend reading the spec on that and the tips before doing multi-threaded JNI. Then you can consider using either Executor or attach_current_thread to get appropriate JNIEnv, and use |
Beta Was this translation helpful? Give feedback.
-
Here is an example to meet the original requirement: A previous commit to save JNIEnv thread locally is significant also. |
Beta Was this translation helpful? Give feedback.
Here is an example to meet the original requirement:
apache/opendal#2291
A previous commit to save JNIEnv thread locally is significant also.