//创建一个线程,线程名称为 thread1, 堆栈大小为4k
let new_thread_result1 = thread::Builder::new()
.name("thread1".to_string())
.stack_size(4 * 1024 * 1024)
.spawn(move || for i in 1..10 {
thread::spawn(move || println!("I am son thread. i={:?} ", i));
});
在子线程中启动子线程:
let new_thread_result2 = thread::Builder::new()
.name("thread1".to_string())
.stack_size(4 * 1024 * 1024)
.spawn(move || for i in 1..10 {
thread::spawn(move || for j in 1..5 {
thread::Builder::new()
.name(i.to_string() + &String::from("_") + &j.to_string())
.spawn(move || println!("i=>{:?} j:{:?}", i, j));
});
});
当然,上面可以写得简单,直接
let new_thread_result2 = thread::spawn(move || for i in 1..10 {
thread::spawn(move || for j in 1..5 {
thread::spawn(move || println!("i=>{:?} j:{:?}", i, j));
});
});