Golang中如何暂停协程直到用户输入后再并发执行多个协程?
Golang中如何暂停协程直到用户输入后再并发执行多个协程? 我的代码旨在通过将切片分解为4个子切片来对数字切片进行排序,然后每个goroutine对一个子切片进行排序。用户将输入数字切片。但我的代码在输入之前就执行了,我知道goroutines就是为了这个目的设计的,不浪费时间并同时执行。但是否有一种方法可以等待直到输入完成后再执行goroutines?
这是我的代码:
package main
import (
"fmt"
"sync"
)
func main() {
var ele int
var lng int
var waitGroup sync.WaitGroup
num_slice := make([]int,0,3)
fmt.Println("How many elements do you want in your slice? ")
fmt.Scan(lng)
fmt.Println("Enter a slice of 10 elements:")
for m := 0; m < lng; m++{
fmt.Scan(&ele)
num_slice = append(num_slice, ele)
}
fmt.Println(num_slice)
s:= make(chan int)
subSlice := lng / 4
slice1 := num_slice[ :subSlice]
slice2 := num_slice[subSlice : 2*(subSlice)]
slice3 := num_slice[2*(subSlice) : 3*(subSlice)]
slice4 := num_slice[3*(subSlice): ]
waitGroup.Add(4)
go sortedSub(slice1, s)
waitGroup.Done()
go sortedSub(slice2, s)
waitGroup.Done()
go sortedSub(slice3, s)
waitGroup.Done()
go sortedSub(slice4, s)
waitGroup.Done()
waitGroup.Wait()
fmt.Println("The final:")
fmt.Println(s)
fmt.Println(slice1)
fmt.Println(slice2)
fmt.Println(slice3)
fmt.Println(slice4)
}
func sortedSub(sl []int, s chan int){
el := 0
fmt.Println("subslice:", sl)
Sort(sl)
for _, v := range sl {
el=v
s <- el
}
}
func Sort(sl []int) {
for i := 0; i < len(sl); i++ {
for j := 0; j < len(sl)-1; j++ {
if sl[j] > sl[j+1] {
Swap(sl, j)
}
}
}
}
func Swap(sl []int, position int) {
temp := sl[position+1]
sl[position+1] = sl[position]
sl[position] = temp
}
输出:
C:\Users\HOME PC\Desktop\Go>sort4
How many elements do you want in your slice?
Enter a slice of 10 elements:
[]
The final:
0xc00003c0c0
[]
[]
[]
[]
C:\Users\HOME PC\Desktop\Go>
更多关于Golang中如何暂停协程直到用户输入后再并发执行多个协程?的实战教程也可以访问 https://www.itying.com/category-94-b0.html
3 回复
亲爱的 lutzhorn,
非常感谢!!它起作用了,如果您能解释一下它是如何工作的,我将非常高兴。
更多关于Golang中如何暂停协程直到用户输入后再并发执行多个协程?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
这不可能是个问题。在通过 go sortedSub(...) 启动任何 Goroutine 之前,你的代码就已经完成了对 fmt.Scan 的调用。
这才是你的问题:
fmt.Scan(lng)
尝试改用类似这样的方式:
_, err := fmt.Scanf("%d", &lng)
if err != nil {
log.Panic("that's not an integer")
}


