同时计算树叶
Count Tree Leaves Concurrently
我想使用并发模型编写一个函数,以防输入太大,并行处理会更有效,但它永远不会结束。
假设有一个 struct
定义为:
type Tree struct {
Name string `json:"name"`
SubTrees []*Tree `json:"subTrees,omitempty"`
Leaves []string `json:"leaves"`
}
我想写一个函数来计算整个递归结构中Leaves
的总数。这很容易通过递归完成:
func (tree *Tree) CountLeaves() int {
curr := len(tree.Leaves)
for _, s := range tree.SubTrees {
curr += s.CountLeaves()
}
return curr
}
这很好,但是如果结构变得太大,这将是低效的,所以我想将它重构为并发并使用通道。这是我对重构的尝试:
func (tree *Tree) CountLeaves() int {
var wg sync.WaitGroup
ch := make(chan int)
defer close(ch)
go count(tree, true, ch, &wg)
var total int
wg.Add(1)
go func(total *int) {
for x := range ch {
fmt.Println(x)
*total += x
}
wg.Done()
}(&total)
wg.Wait()
return total
}
func count(t *Tree, root bool, ch chan int, wg *sync.WaitGroup) {
defer wg.Done()
ch <- len(t.Leaves)
if t.SubTrees != nil {
wg.Add(len(t.SubTrees))
for _, s := range t.SubTrees {
go count(s, false, ch, wg)
}
wg.Wait()
}
if root {
ch <- -1
}
}
我目前能够通过渠道收集所有数字,我目前需要计算 Leaves
的总数,但该功能永远不会结束。来自根 Tree
结构的终止值 -1
永远不会通过通道推送或接收,我不明白为什么。
有什么想法吗?
我很确定你的 WaitGroup
永远不会收到足够的 wg.Done
电话:
go func(total *int) {
for x := range ch {
fmt.Println(x)
*total += x
}
wg.Done()
}(&total)
因为你永远不会关闭 ch
,wg.Done
永远不会在这里被调用。我认为如果你把它移到循环内:
go func(total *int) {
for x := range ch {
fmt.Println(x)
*total += x
wg.Done()
}
}(&total)
这将解决问题。
编辑:
其实我觉得还有一个问题:
defer wg.Done()
ch <- len(t.Leaves)
if t.SubTrees != nil {
wg.Add(len(t.SubTrees))
for _, s := range t.SubTrees {
go count(s, false, ch, wg)
}
wg.Wait()
}
延迟的 wg.Done()
直到您 return 才会被调用,所以这个 wg.Wait()
也将永远等待。这可能是:
ch <- len(t.Leaves)
if t.SubTrees != nil {
wg.Add(len(t.SubTrees))
for _, s := range t.SubTrees {
go count(s, false, ch, wg)
}
wg.Done()
wg.Wait()
} else {
wg.Done()
}
我想使用并发模型编写一个函数,以防输入太大,并行处理会更有效,但它永远不会结束。
假设有一个 struct
定义为:
type Tree struct {
Name string `json:"name"`
SubTrees []*Tree `json:"subTrees,omitempty"`
Leaves []string `json:"leaves"`
}
我想写一个函数来计算整个递归结构中Leaves
的总数。这很容易通过递归完成:
func (tree *Tree) CountLeaves() int {
curr := len(tree.Leaves)
for _, s := range tree.SubTrees {
curr += s.CountLeaves()
}
return curr
}
这很好,但是如果结构变得太大,这将是低效的,所以我想将它重构为并发并使用通道。这是我对重构的尝试:
func (tree *Tree) CountLeaves() int {
var wg sync.WaitGroup
ch := make(chan int)
defer close(ch)
go count(tree, true, ch, &wg)
var total int
wg.Add(1)
go func(total *int) {
for x := range ch {
fmt.Println(x)
*total += x
}
wg.Done()
}(&total)
wg.Wait()
return total
}
func count(t *Tree, root bool, ch chan int, wg *sync.WaitGroup) {
defer wg.Done()
ch <- len(t.Leaves)
if t.SubTrees != nil {
wg.Add(len(t.SubTrees))
for _, s := range t.SubTrees {
go count(s, false, ch, wg)
}
wg.Wait()
}
if root {
ch <- -1
}
}
我目前能够通过渠道收集所有数字,我目前需要计算 Leaves
的总数,但该功能永远不会结束。来自根 Tree
结构的终止值 -1
永远不会通过通道推送或接收,我不明白为什么。
有什么想法吗?
我很确定你的 WaitGroup
永远不会收到足够的 wg.Done
电话:
go func(total *int) {
for x := range ch {
fmt.Println(x)
*total += x
}
wg.Done()
}(&total)
因为你永远不会关闭 ch
,wg.Done
永远不会在这里被调用。我认为如果你把它移到循环内:
go func(total *int) {
for x := range ch {
fmt.Println(x)
*total += x
wg.Done()
}
}(&total)
这将解决问题。
编辑:
其实我觉得还有一个问题:
defer wg.Done()
ch <- len(t.Leaves)
if t.SubTrees != nil {
wg.Add(len(t.SubTrees))
for _, s := range t.SubTrees {
go count(s, false, ch, wg)
}
wg.Wait()
}
延迟的 wg.Done()
直到您 return 才会被调用,所以这个 wg.Wait()
也将永远等待。这可能是:
ch <- len(t.Leaves)
if t.SubTrees != nil {
wg.Add(len(t.SubTrees))
for _, s := range t.SubTrees {
go count(s, false, ch, wg)
}
wg.Done()
wg.Wait()
} else {
wg.Done()
}