Goroutines:在哪里关闭

我很难理解我应该在哪里关闭我的频道。


这段代码大约需要 0.7 秒:


options := [3]string{"0", "1", "2"}

str := fmt.Sprintf("%6d ", id)

for j := 0; j < 40000; j++ {

    str += options[rand.Intn(3)]

}

str += "\n"

添加 io.Writestring 对时间没有影响,所以问题出在这一点。


我想要大约 100,000 条这样的记录,所以我想放入一个 goroutine。


func main() {

    file, _ := os.Create("myfile.txt")

    ch := make(chan string)

    for i := 0; i < 100000; i++ {

       go generate(i, ch)

    }


    counter := 0

    for result := range ch {

       counter++

       io.WriteString(file, result)

       if counter == 100000 {

           close(ch)

       }

    }

    file.Close()

}


func generate(id int, c chan string) {

    options := [3]string{"0", "1", "2"}

    str := fmt.Sprintf("%6d ", id)

    for j := 0; j < 40000; j++ {

        str += options[rand.Intn(3)]

    }

    str += "\n"

    c <- str

}

据我了解,我正在关闭接收方的通道,这并不理想?此外,这样所有 100,000 应该首先发送到 goroutines,然后我才能收到任何。我可以发送请求以生成记录并同时开始接收吗?


慕婉清6462132
浏览 125回答 2
2回答

qq_遁去的一_1

使用计数器关闭您的频道不是一个好习惯。您可以使用sync.WaitGroup. 这使您可以更好地控制何时关闭频道:func main() {&nbsp; &nbsp; var wg sync.WaitGroup&nbsp; &nbsp; ch := make(chan string)&nbsp; &nbsp; file, _ := os.Create("myfile.txt")&nbsp; &nbsp; for i := 0; i < 100000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go func(i int) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options := [3]string{"0", "1", "2"}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str := fmt.Sprintf("%6d ", i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for j := 0; j < 40000; j++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str += options[rand.Intn(3)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str += "\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ch <- str&nbsp; &nbsp; &nbsp; &nbsp; }(i)&nbsp; &nbsp; }&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; &nbsp; &nbsp; close(ch)&nbsp; &nbsp; }()&nbsp; &nbsp; for result := range ch {&nbsp; &nbsp; &nbsp; &nbsp; io.WriteString(file, result)&nbsp; &nbsp; }&nbsp; &nbsp; file.Close()}

POPMUISE

看看能不能解决你的问题。。func main() {&nbsp; &nbsp; file, _ := os.Create("myfile.txt")&nbsp; &nbsp; ch := make(chan string)&nbsp; &nbsp; wg := new(sync.WaitGroup)&nbsp; &nbsp; for i := 0; i < 100000; i++ {&nbsp; &nbsp; &nbsp; &nbsp;wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp;go generate(i, ch)&nbsp; &nbsp; }&nbsp; &nbsp; go func(){wg.Wait();close(ch)}()&nbsp; &nbsp; counter := 0&nbsp; &nbsp; for result := range ch {&nbsp; &nbsp; &nbsp; &nbsp;counter++&nbsp; &nbsp; &nbsp; &nbsp;io.WriteString(file, result)&nbsp; &nbsp; }&nbsp; &nbsp; file.Close()}func generate(id int, c chan string) {&nbsp; &nbsp; options := [3]string{"0", "1", "2"}&nbsp; &nbsp; str := fmt.Sprintf("%6d ", id)&nbsp; &nbsp; for j := 0; j < 40000; j++ {&nbsp; &nbsp; &nbsp; &nbsp; str += options[rand.Intn(3)]&nbsp; &nbsp; }&nbsp; &nbsp; str += "\n"&nbsp; &nbsp; c <- str&nbsp; &nbsp; wg.Done()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go