From 521b23770c94028e973672d1953c42d198df754f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 1 May 2020 21:03:10 +0800 Subject: [PATCH] sleep sort --- thread/src/main.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/thread/src/main.rs b/thread/src/main.rs index 553f06b..90609d8 100644 --- a/thread/src/main.rs +++ b/thread/src/main.rs @@ -1,10 +1,20 @@ -use std::thread; +use std::{ thread, time::Duration, }; -// https://zhuanlan.zhihu.com/p/89716165 +// sleep sort fn main() { - let child = thread::spawn(move || { - println!("Hello World!"); - }); - let _res = child.join().unwrap(); + let in_vec = vec![2, 1, 5, 10, 7]; + + let mut all_threads = vec![]; + for i in in_vec { + let child = thread::spawn(move || { + thread::sleep(Duration::from_millis(i)); + println!(">> {}", i); + }); + all_threads.push(child); + } + for t in all_threads { + t.join().ok(); + } + println!("Finished!"); }