Updated 'src/concurrency/threads'.

This commit is contained in:
Hector PENG
2026-04-06 15:56:56 +08:00
parent d324352d2b
commit a2db11c0eb
7 changed files with 229 additions and 168 deletions

View File

@@ -0,0 +1,6 @@
[package]
name = "spawn_demo"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -0,0 +1,18 @@
use std::thread;
use std::time::Duration;
fn main() {
let handle = thread::spawn(|| {
for i in 1..10 {
println! ("hi生成的线程中的数字 {i} !");
thread::sleep(Duration::from_millis(1));
}
});
handle.join().unwrap();
for i in 1..5 {
println! ("hi主线程中的数字 {i} !");
thread::sleep(Duration::from_millis(1));
}
}

View File

@@ -0,0 +1,6 @@
[package]
name = "threads"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@@ -0,0 +1,13 @@
use std::thread;
fn main() {
let v = vec! [1, 2, 3];
let handle = thread::spawn(move || {
println! ("这里有个矢量值:{v:?}");
});
drop(v);
handle.join().unwrap();
}