From 265eef56271c5baf0c6f09827f5e979286ad216e Mon Sep 17 00:00:00 2001 From: yangyudong <916291030@qq.com> Date: Fri, 18 Apr 2025 22:00:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=B9=B6=E5=8F=91=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=B4=A0=E6=95=B0=E8=AE=A1=E7=AE=97=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=B7=A5=E4=BD=9C=E5=8D=8F=E7=A8=8B?= =?UTF-8?q?=E5=92=8C=E7=BB=93=E6=9E=9C=E9=80=9A=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../05_concurrency/01_goroutines/main.go | 27 ++++++++- .../05_concurrency/04_more/go.mod | 3 + .../05_concurrency/04_more/main.go | 56 +++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 02_core_concepts/05_concurrency/04_more/go.mod create mode 100644 02_core_concepts/05_concurrency/04_more/main.go diff --git a/02_core_concepts/05_concurrency/01_goroutines/main.go b/02_core_concepts/05_concurrency/01_goroutines/main.go index 0db886e..0d07390 100644 --- a/02_core_concepts/05_concurrency/01_goroutines/main.go +++ b/02_core_concepts/05_concurrency/01_goroutines/main.go @@ -1,5 +1,28 @@ package main -func main() { - // TODO +import ( + "fmt" + "time" +) + +// 模拟一个耗时的计算任务 +func heavyTask(id int) { + fmt.Printf("任务 %d 开始执行\n", id) + time.Sleep(2 * time.Second) + fmt.Printf("任务 %d 执行完成\n", id) +} + +func main() { + fmt.Println("程序开始...") + + // 启动200个goroutine + for i := 1; i <= 200; i++ { + go heavyTask(i) + } + + // 主线程等待一段时间,确保goroutine有足够时间执行 + // 注意:在实际生产环境中应该使用更好的同步机制 + time.Sleep(3 * time.Second) + + fmt.Println("程序结束...") } diff --git a/02_core_concepts/05_concurrency/04_more/go.mod b/02_core_concepts/05_concurrency/04_more/go.mod new file mode 100644 index 0000000..042f49b --- /dev/null +++ b/02_core_concepts/05_concurrency/04_more/go.mod @@ -0,0 +1,3 @@ +module morecon + +go 1.24.1 diff --git a/02_core_concepts/05_concurrency/04_more/main.go b/02_core_concepts/05_concurrency/04_more/main.go new file mode 100644 index 0000000..ba7d56c --- /dev/null +++ b/02_core_concepts/05_concurrency/04_more/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "math" +) + +// 判断一个数是否为素数 +func isPrime(n int) bool { + if n <= 1 { + return false + } + sqrt := int(math.Sqrt(float64(n))) + for i := 2; i <= sqrt; i++ { + if n%i == 0 { + return false + } + } + return true +} + +func worker(id int, jobs <-chan int, results chan<- int) { + for job := range jobs { + fmt.Printf("Worker %d processing job %d\n", id, job) + // 计算从job开始的前100个数中有多少个素数 + count := 0 + for i := job; i < job+100; i++ { + if isPrime(i) { + count++ + } + } + results <- count // 将结果发送到results通道 + } +} + +func main() { + jobs := make(chan int, 100) // 工作通道 + results := make(chan int, 100) // 结果通道 + + // 启动3个工作协程 + for w := 1; w <= 3; w++ { + go worker(w, jobs, results) + } + + // 发送工作任务 + for j := 1; j <= 80; j++ { + jobs <- j + } + close(jobs) + + // 收集结果 + for i := 1; i <= 80; i++ { + result := <-results + fmt.Printf("Got result: %d\n", result) // 输出每个范围内的素数个数 + } +}