feat: add select3
This commit is contained in:
40
__concurrent/async_study/examples/select3.rs
Normal file
40
__concurrent/async_study/examples/select3.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::{join, select};
|
||||
use tokio::sync::oneshot::channel;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let (tx1, rx1) = channel::<i32>();
|
||||
|
||||
let a = tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_millis(2000)).await;
|
||||
let _ = tx1.send(1);
|
||||
println!(">>> send tx1");
|
||||
});
|
||||
|
||||
select! {
|
||||
a = rx1 => {
|
||||
println!("tx: {:?}", a);
|
||||
}
|
||||
b = run() => {
|
||||
println!("run: {:?}", b);
|
||||
}
|
||||
c = tokio::time::sleep(Duration::from_millis(1500)) => {
|
||||
println!("timeout: {:?}", c);
|
||||
}
|
||||
}
|
||||
|
||||
println!("join: {:?}", join!(a));
|
||||
|
||||
println!(">> pre end");
|
||||
tokio::time::sleep(Duration::from_millis(2000)).await;
|
||||
println!(">> end");
|
||||
}
|
||||
|
||||
async fn run() {
|
||||
for i in 0.. {
|
||||
tokio::time::sleep(Duration::from_millis(300)).await;
|
||||
println!("# {}", i);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user