做纯粹的自己。“你要搞清楚自己人生的剧本——不是父母的续集,不是子女的前传,更不是朋友的外篇。对待生命你不妨再大胆一点,因为你好歹要失去它。如果这世上真有奇迹,那只是努力的另一个名字”。
一、crossbeam_channel
参考 crossbeam_channel - Rust
crossbeam_channel 是一个多生产者多消费者通道,用于消息传递,它是std::sync::mpsc的替代品,具有更多的功能和更好的性能。
二、Channel 类型
通道可以使用两个函数创建:
- bounded 函数创建一个容量有限的信道,即一个信道一次可以容纳的消息数量是有限制的。
- unbounded 函数创建一个容量无界的信道,即它一次可以容纳任意数量的消息。
这两个函数都返回一个发送方 Sender 和一个接收方 Receiver,它们代表通道的相反两端。
创建一个有界 Channel:
use crossbeam_channel::bounded;// Create a channel that can hold at most 5 messages at a time.
let (s, r) = bounded(5);// Can send only 5 messages without blocking.
for i in 0..5 {s.send(i).unwrap();
}// Another call to `send` would block because the channel is full.
// s.send(5).unwrap();
创建一个无界 Channel:
use crossbeam_channel::unbounded;// Create an unbounded channel.
let (s, r) = unbounded();// Can send any number of messages into the channel without blocking.
for i in 0..1000 {s.send(i).unwrap();
}
三、通过 JNI 使用 Channel
Java 端可以通过 JNI 调用 getSender
获取发送端指针,调用 sendMessage
发送消息到 Rust 中的处理线程,由 Rust 负责处理核心逻辑。
1、新建一个 Rust 库项目
cargo new rust_jni_channel_test --lib
添加依赖包,
# Cargo.toml[dependencies]
jni = "0.21.1"
lazy_static = "1.5.0"
crossbeam-channel = "0.5.13"
#log = "0.4"
#env_logger = "0.11"[lib]
crate_type = ["cdylib"]
实现 JNI 模块函数,
// lib.rs#[macro_use]
extern crate lazy_static;use jni::objects::{JClass, JObject};
use jni::sys::{jlong, jobject};
use jni::JNIEnv;
use crossbeam_channel::{unbounded, Sender, Receiver};
use std::thread;lazy_static! {static ref SENDER: Sender<String> = {let (sender, receiver) = unbounded();// Spawn a thread to handle the receiverthread::spawn(move || {for message in receiver.iter() {println!("Received message: {}", message);}});sender};
}#[no_mangle]
pub extern "system" fn Java_com_yushanma_MyResultHandler_getSender(_env: JNIEnv,_class: JClass,
) -> jlong {let sender_ptr = Box::into_raw(Box::new(SENDER.clone())) as jlong;sender_ptr
}#[no_mangle]
pub extern "system" fn Java_com_yushanma_MyResultHandler_sendMessage(mut env: JNIEnv,_class: JClass,sender_ptr: jlong,message: JObject,
) {let sender = unsafe { &*(sender_ptr as *mut Sender<String>) };let message: String = env.get_string(&message.into()).expect("Couldn't get java