From 28b05c6d2630ea9a211ea3f9e79e28e11556437b Mon Sep 17 00:00:00 2001 From: TimLai666 <43640816+TimLai666@users.noreply.github.com> Date: Tue, 18 Jun 2024 18:52:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BF=BB=E8=AD=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/20_threads/README.md | 13 ++++++------- exercises/20_threads/threads1.rs | 15 ++++++--------- exercises/20_threads/threads2.rs | 15 ++++++--------- exercises/20_threads/threads3.rs | 3 +-- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/exercises/20_threads/README.md b/exercises/20_threads/README.md index 0b32fb1d..47dcce1b 100644 --- a/exercises/20_threads/README.md +++ b/exercises/20_threads/README.md @@ -1,10 +1,9 @@ -# Threads +# 執行緒(Threads) -In most current operating systems, an executed program's code is run in a process, and the operating system manages multiple processes at once. -Within your program, you can also have independent parts that run simultaneously. The features that run these independent parts are called threads. +在大多數現代作業系統中,執行程式的代碼在一個進程中運行,作業系統同時管理多個進程。在您的程式中,您還可以有同時運行的獨立部分。這些獨立運行部分的功能稱為執行緒。 -## Further information +## 進一步了解 -- [Dining Philosophers example](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html) -- [Using Threads to Run Code Simultaneously](https://doc.rust-lang.org/book/ch16-01-threads.html) -- [Using Message Passing to Transfer Data Between Threads](https://doc.rust-lang.org/book/ch16-02-message-passing.html) +- [哲學家就餐問題示例](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html) +- [使用執行緒同時運行代碼](https://doc.rust-lang.org/book/ch16-01-threads.html) +- [使用訊息傳遞在執行緒之間傳輸資料](https://doc.rust-lang.org/book/ch16-02-message-passing.html) diff --git a/exercises/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index 80b6def3..72b7232f 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_threads/threads1.rs @@ -1,12 +1,9 @@ // threads1.rs // -// This program spawns multiple threads that each run for at least 250ms, and -// each thread returns how much time they took to complete. The program should -// wait until all the spawned threads have finished and should collect their -// return values into a vector. +// 這個程式會生成多個執行緒,每個執行緒至少執行250毫秒,並回傳它們完成所需的時間。 +// 程式應該等待所有生成的執行緒完成,並將它們的回傳值收集到一個向量中。 // -// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a -// hint. +// 執行 `rustlings hint threads1` 或使用 `hint` watch 子命令以獲取提示。 // I AM NOT DONE @@ -26,15 +23,15 @@ fn main() { let mut results: Vec = vec![]; for handle in handles { - // TODO: a struct is returned from thread::spawn, can you use it? + // TODO: 有一個結構體是從 thread::spawn 回傳的,你能使用它嗎? } if results.len() != 10 { - panic!("Oh no! All the spawned threads did not finish!"); + panic!("哦不!所有生成的執行緒都沒有完成!"); } println!(); for (i, result) in results.into_iter().enumerate() { - println!("thread {} took {}ms", i, result); + println!("thread {} 花費了 {} 毫秒", i, result); } } diff --git a/exercises/20_threads/threads2.rs b/exercises/20_threads/threads2.rs index 60d68241..357f0045 100644 --- a/exercises/20_threads/threads2.rs +++ b/exercises/20_threads/threads2.rs @@ -1,11 +1,8 @@ // threads2.rs // -// Building on the last exercise, we want all of the threads to complete their -// work but this time the spawned threads need to be in charge of updating a -// shared value: JobStatus.jobs_completed +// 基於上一個練習,我們希望所有執行緒完成他們的工作,但這次生成的執行緒需要負責更新一個共享的值:JobStatus.jobs_completed // -// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a -// hint. +// 執行 `rustlings hint threads2` 或使用 `hint` watch 子命令以獲取提示。 // I AM NOT DONE @@ -18,7 +15,7 @@ struct JobStatus { } fn main() { - // TODO: `Arc` isn't enough if you want a **mutable** shared state + // TODO: 如果你想要一個**可變的**共享狀態,僅僅使用 `Arc` 是不夠的 let status = Arc::new(JobStatus { jobs_completed: 0 }); let mut handles = vec![]; @@ -26,17 +23,17 @@ fn main() { let status_shared = Arc::clone(&status); let handle = thread::spawn(move || { thread::sleep(Duration::from_millis(250)); - // TODO: You must take an action before you update a shared value + // TODO: 在更新共享值之前,你必須採取一個動作 status_shared.jobs_completed += 1; }); handles.push(handle); } - // Waiting for all jobs to complete + // 等待所有工作完成 for handle in handles { handle.join().unwrap(); } - // TODO: Print the value of `JobStatus.jobs_completed` + // TODO: 印出 `JobStatus.jobs_completed` 的值 println!("Jobs completed: {}", ???); } diff --git a/exercises/20_threads/threads3.rs b/exercises/20_threads/threads3.rs index acb97b4b..bc5879bc 100644 --- a/exercises/20_threads/threads3.rs +++ b/exercises/20_threads/threads3.rs @@ -1,7 +1,6 @@ // threads3.rs // -// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a -// hint. +// 執行 `rustlings hint threads3` 或使用 `hint` watch 子命令以獲取提示。 // I AM NOT DONE