-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy paths010_thread_ctl.rs
More file actions
53 lines (50 loc) · 1.61 KB
/
s010_thread_ctl.rs
File metadata and controls
53 lines (50 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use jni::objects::{JClass, JString};
use jni::sys::jboolean;
use jni::JNIEnv;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::thread;
use std::time::{Duration, Instant};
static ACTIVE: Mutex<AtomicBool> = Mutex::new(AtomicBool::new(true));
#[no_mangle]
pub extern "system" fn Java_sample_s010_RunInRustThread_callAsync<'local>(
mut env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
j_msg: JString<'local>,
) {
let recv_msg = if let Ok(msg) = env.get_string(&j_msg) {
String::from(msg)
} else {
String::from("failed to get java string")
};
thread::spawn(move || loop {
if let Ok(active) = ACTIVE.lock() {
if active.load(Ordering::SeqCst) {
println!("{}, {:?}", recv_msg, Instant::now());
} else {
println!("quit");
break;
}
}
thread::sleep(Duration::from_millis(50));
});
}
#[no_mangle]
pub extern "system" fn Java_sample_s010_RunInRustThread_stopExecute<'local>(
_env: JNIEnv<'local>,
// This is the class that owns our static method. It's not going to be used,
// but still must be present to match the expected signature of a static
// native method.
_class: JClass<'local>,
) -> jboolean {
if let Ok(active) = ACTIVE.lock() {
active.store(false, Ordering::SeqCst);
1
} else {
println!("failed to lock active");
0
}
}