feat: add concurrent/waitgroup demo
This commit is contained in:
26
__concurrent/waitgroup-demo/src/main.rs
Normal file
26
__concurrent/waitgroup-demo/src/main.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::task::yield_now;
|
||||
use tokio::time;
|
||||
use tokio::time::Instant;
|
||||
use waitgroup::WaitGroup;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let wg = WaitGroup::new();
|
||||
for i in 80..100 {
|
||||
let w = wg.worker();
|
||||
tokio::spawn(async move {
|
||||
println!("Start work #{}", i);
|
||||
time::sleep(Duration::from_millis(i * 100)).await;
|
||||
println!("Finish work #{}", i);
|
||||
drop(w);
|
||||
});
|
||||
yield_now().await;
|
||||
}
|
||||
|
||||
let instant = Instant::now();
|
||||
println!("Start waiting...");
|
||||
wg.wait().await;
|
||||
println!("Finish wait, elapsed: {}ms", instant.elapsed().as_millis());
|
||||
}
|
||||
Reference in New Issue
Block a user