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