Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 如何等待该计划的实施_Multithreading_Go_Concurrency - Fatal编程技术网

Multithreading 如何等待该计划的实施

Multithreading 如何等待该计划的实施,multithreading,go,concurrency,Multithreading,Go,Concurrency,我有一个大的日志文件,你想并行分析 我有以下代码: package main import ( "bufio" "fmt" "os" "time" ) func main() { filename := "log.txt" threads := 10 // Read the file file, err := os.Open(filename) if err != nil { fmt.Println(

我有一个大的日志文件,你想并行分析

我有以下代码:

package main

import (
    "bufio"
    "fmt"
    "os"
    "time"
)

func main() {
    filename := "log.txt"
    threads := 10

    // Read the  file
    file, err := os.Open(filename)
    if err != nil {
        fmt.Println("Could not open file with the database.")
        os.Exit(1)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)

    // Channel for strings
    tasks := make(chan string)

    // Run the threads that catch events from the channel and understand one line of the log file
    for i := 0; i < threads; i++ {
        go parseStrings(tasks)
    }

    // Start a thread load lines from a file into the channel
    go getStrings(scanner, tasks)

    // At this point I have to wait until all of the threads executed
    // For example, I set the sleep
    for {
        time.Sleep(1 * time.Second)
    }
}

func getStrings(scanner *bufio.Scanner, tasks chan<- string) {
    for scanner.Scan() {
        s := scanner.Text()
        tasks <- s
    }
}

func parseStrings(tasks <-chan string) {
    for {
        s := <-tasks
        event := parseLine(s)
        fmt.Println(event)
    }
}

func parseLine(line string) string {
    return line
}
主程序包
进口(
“布菲奥”
“fmt”
“操作系统”
“时间”
)
func main(){
文件名:=“log.txt”
线程数:=10
//读文件
文件,错误:=os.Open(文件名)
如果错误!=零{
fmt.Println(“无法打开数据库中的文件。”)
操作系统退出(1)
}
延迟文件。关闭()
扫描程序:=bufio.NewScanner(文件)
//字符串通道
任务:=make(chan字符串)
//运行从通道捕获事件的线程并理解日志文件的一行
对于i:=0;i
启动每个goroutine时,请执行以下操作:

wg.Add(1)
当goroutine工作完成时,使用减量计数器

wg.Done()
因此,而不是

for {
    time.Sleep(1 * time.Second)
}

var wg sync.WaitGroup

启动每个goroutine时,请执行以下操作:

wg.Add(1)
当goroutine工作完成时,使用减量计数器

wg.Done()
因此,而不是

for {
    time.Sleep(1 * time.Second)
}

只要使用

主程序包
进口(
“同步”
)
func stuff(wg*sync.WaitGroup){
defer wg.Done()//告诉WaitGroup已经完成
/*东西*/
}
func main(){
计数:=50
wg:=新建(sync.WaitGroup)
wg.Add(count)//将gorutine的数量添加到WaitGroup
对于i:=0;i

只需使用

主程序包
进口(
“同步”
)
func stuff(wg*sync.WaitGroup){
defer wg.Done()//告诉WaitGroup已经完成
/*东西*/
}
func main(){
计数:=50
wg:=新建(sync.WaitGroup)
wg.Add(count)//将gorutine的数量添加到WaitGroup
对于i:=0;i

使用管道模式和“扇出/扇入”模式:

主程序包
进口(
“布菲奥”
“fmt”
“字符串”
“同步”
)
func main(){
文件:=“这是第一行\n”+
“这是第二行\n”+
“这是第3行\n”+
“这是第4行\n”+
“这是第5行\n”+
“这是第6行\n”+
“这是第7行\n”
scanner:=bufio.NewScanner(strings.NewReader(文件))
//将所有线路连接到一个通道
in:=GetString(扫描仪)
//散开
//从同一通道读取多个函数,直到该通道关闭
//将工作分配到多个函数(十个goroutine),这些函数都从中读取。
xc:=扇出(英寸,10)
//扇入
//将多个通道多路复用到单个通道上
//将从c0到c9的通道合并到单个通道上
对于n:=范围合并(xc){
fmt.Println(n)
}
}

func getStrings(scanner*bufio.scanner)使用管道模式和“扇出/扇入”模式:

主程序包
进口(
“布菲奥”
“fmt”
“字符串”
“同步”
)
func main(){
文件:=“这是第一行\n”+
“这是第二行\n”+
“这是第3行\n”+
“这是第4行\n”+
“这是第5行\n”+
“这是第6行\n”+
“这是第7行\n”
scanner:=bufio.NewScanner(strings.NewReader(文件))
//将所有线路连接到一个通道
in:=GetString(扫描仪)
//散开
//从同一通道读取多个函数,直到该通道关闭
//将工作分配到多个函数(十个goroutine),这些函数都从中读取。
xc:=扇出(英寸,10)
//扇入
//将多个通道多路复用到单个通道上
//将从c0到c9的通道合并到单个通道上
对于n:=范围合并(xc){
fmt.Println(n)
}
}

func getStrings(scanner*bufio.scanner)?@Ainar-G我不知道如何正确添加?@Ainar-G我不知道如何正确添加