意义雷同于单核多线程的含义
单线程下,可以进行多个异步任务。然后这些异步任务可以是网络请求,这样达到单线层的最大利用化。
extern crate core;
use futures::future::join_all;
use std::time::Instant;
async fn sleep_ms(start: &Instant, id: u64, duration_ms: u64) {
tokio::time::sleep(std::time::Duration::from_millis(duration_ms)).await;
println!(
"future {id} slept for {duration_ms}ms, finished after {}ms",
start.elapsed().as_millis()
);
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let start = Instant::now();
let sleep_futures = (1..=10).map(|t| sleep_ms(&start, t, t * 10));
join_all(sleep_futures).await;
}
sleep_ms的代码可以是网络请求