astonbitecode / j4rs

Java for Rust
Apache License 2.0
590 stars 35 forks source link

How to use in multi-threaded(sync) #36

Closed sirodeneko closed 2 years ago

sirodeneko commented 2 years ago

I have a structure, then I will instantiate a unique one of it and pass it to sync to call the java method。

pub struct Hannanum {
    _jvm: Jvm,
    _instance: Instance,
}

but,compilation has failed.

   `*mut *const JNINativeInterface_` cannot be sent between threads safely
   = help: within `konlpy::KonlpyTokenizer`, the trait `Send` is not implemented for `*mut *const JNINativeInterface_`
   = note: required because it appears within the type `Jvm`

because I have to implement the 'Send' trait。 I don't know how to implement it safely。

astonbitecode commented 2 years ago

The Jvm cannot be sent between threads because it contains JNI components that also cannot be sent between threads. All you need to do, is to attach the current thread to the Jvm, using:

let jvm: Jvm = Jvm::attach_thread().unwrap();
sirodeneko commented 2 years ago

3Q