rust 学习 多线程1

rust 学习 多线程1
use std::thread; use std::time::Duration; use std::sync::mpsc; fn main() { let handle = thread::spawn(|| { for i in 1..10 { println!("{} in spawn thread!",i); thread::sleep(Duration::from_millis(1)); } }); handle.join().unwrap(); //等待上面的完成才进行下面的 for i in 1..5 { println!("{} in main thread!",i); thread::sleep(Duration::from_millis(1)); } println!("Hello, World"); // handle.join().unwrap(); //等待所有完成 let (tx,rx) = mpsc::channel(); //声明一个通道 thread::spawn(move || { let val = String::from("hi"); tx.send(val).unwrap(); }); let re = rx.recv().unwrap(); println!("got: {}",re);