diff --git a/__concurrent/async_study/examples/select3.rs b/__concurrent/async_study/examples/select3.rs new file mode 100644 index 0000000..63da2af --- /dev/null +++ b/__concurrent/async_study/examples/select3.rs @@ -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::(); + + 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); + } +}