Finish thread1
This commit is contained in:
@@ -13,19 +13,22 @@
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn thread_func(i: u32) -> u128 {
|
||||
let start = Instant::now();
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
println!("thread {} is complete", i);
|
||||
start.elapsed().as_millis()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut handles = vec![];
|
||||
for i in 0..10 {
|
||||
handles.push(thread::spawn(move || {
|
||||
let start = Instant::now();
|
||||
thread::sleep(Duration::from_millis(250));
|
||||
println!("thread {} is complete", i);
|
||||
start.elapsed().as_millis()
|
||||
}));
|
||||
handles.push(thread::spawn(move || thread_func(i)));
|
||||
}
|
||||
|
||||
let mut results: Vec<u128> = vec![];
|
||||
for handle in handles {
|
||||
results.push(handle.join().unwrap());
|
||||
// TODO: a struct is returned from thread::spawn, can you use it?
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user