Goroutine 和工作组问题
Goroutine and Workgroup Issue
我正在尝试结合额外作业("one"、"two" 打印一组大作业(“1,2”、“3,4”、“5”) , "three") 并使用 Goroutines 和 Workgroups 来做到这一点。
我期待以下输出(不完全按照这个顺序,但内部工作组应该在外部工作组之前先完成):
Big Job being done: 1,2
1_one
1_two
1_three
2_one
2_two
2_three
Big Job 1,2 is Done!
Big Job being done: 3,4
3_one
3_two
3_three
4_one
4_two
4_three
Big Job 3,4 is Done!
Big Job being done: 5
5_one
5_two
5_three
Big Job 5 is Done!
All Big Jobs are done!
去游乐场Link:https://play.golang.org/p/Hvbcmw06WY
但是,当我 运行 我的代码时,我得到以下输出:
Big job 1,2 is Done!
Big job 3,4 is Done!
Big job 5 is Done!
Big Job being done: 5
5_three
Big Job being done: 1,2
1_three
Big Job being done: 3,4
3_three
5_one
5_two
1_one
1_two
3_one
3_two
fatal error: all goroutines are asleep - deadlock!
如您所见,Big Jobs 的第二部分(2
和 4
)以某种方式 "lost" 在 goroutine 中。
此外,All Big Jobs are done!
消息永远不会到达,因为所有 goroutine 都以某种方式睡着了,即使每个 Waitgroup 应该已经完成(尽管缺少的 2
和 4
部分可能已经完成与它有关)。
我还注意到,出于某种原因,Done
消息首先被打印(尽管我假设 Print 函数只是难以赶上 Goroutines)。
这是我想要完成的非 Goroutine 版本:https://play.golang.org/p/zZpfyIbbn8
知道我做错了什么吗?
jobWG.Wait()
在 for _, job := range jobs
循环内部,而它应该在外部。
这是固定版本https://play.golang.org/p/KNLS0y8xLg
我正在尝试结合额外作业("one"、"two" 打印一组大作业(“1,2”、“3,4”、“5”) , "three") 并使用 Goroutines 和 Workgroups 来做到这一点。
我期待以下输出(不完全按照这个顺序,但内部工作组应该在外部工作组之前先完成):
Big Job being done: 1,2
1_one
1_two
1_three
2_one
2_two
2_three
Big Job 1,2 is Done!
Big Job being done: 3,4
3_one
3_two
3_three
4_one
4_two
4_three
Big Job 3,4 is Done!
Big Job being done: 5
5_one
5_two
5_three
Big Job 5 is Done!
All Big Jobs are done!
去游乐场Link:https://play.golang.org/p/Hvbcmw06WY
但是,当我 运行 我的代码时,我得到以下输出:
Big job 1,2 is Done!
Big job 3,4 is Done!
Big job 5 is Done!
Big Job being done: 5
5_three
Big Job being done: 1,2
1_three
Big Job being done: 3,4
3_three
5_one
5_two
1_one
1_two
3_one
3_two
fatal error: all goroutines are asleep - deadlock!
如您所见,Big Jobs 的第二部分(2
和 4
)以某种方式 "lost" 在 goroutine 中。
此外,All Big Jobs are done!
消息永远不会到达,因为所有 goroutine 都以某种方式睡着了,即使每个 Waitgroup 应该已经完成(尽管缺少的 2
和 4
部分可能已经完成与它有关)。
我还注意到,出于某种原因,Done
消息首先被打印(尽管我假设 Print 函数只是难以赶上 Goroutines)。
这是我想要完成的非 Goroutine 版本:https://play.golang.org/p/zZpfyIbbn8
知道我做错了什么吗?
jobWG.Wait()
在 for _, job := range jobs
循环内部,而它应该在外部。
这是固定版本https://play.golang.org/p/KNLS0y8xLg