gpt4 book ai didi

html - 代码提前结束

转载 作者:数据小太阳 更新时间:2023-10-29 03:04:02 25 4
gpt4 key购买 nike

在我的代码中,代码在执行所有任务之前执行。我必须更改我的代码什么才能在结束前完成所有任务?

package main

import (
"fmt"
"math/rand"
"time"
)

// run x tasks at random intervals
// - a task is a goroutine that runs for 2 seconds.
// - a task runs concurrently to other task
// - the interval between task is between 0 and 2 seconds

func main() {
// set x to the number of tasks
x := 4
// random numbers generation initialization
random := rand.New(rand.NewSource(1234))

for num := 0; num < x; num++ {
// sleep for a random amount of milliseconds before starting a new task
duration := time.Millisecond * time.Duration(random.Intn(2000))
time.Sleep(duration)

// run a task
go func() {
// this is the work, expressed by sleeping for 2 seconds
time.Sleep(2 * time.Second)
fmt.Println("task done")
}()
}
}

最佳答案

是的,正如@Laney 提到的,这可以使用 WaitGroup 和 channel 来完成。请引用下面的代码。

WaitGroup :

package main

import (
"fmt"
"math/rand"
"sync"
"time"
)

// run x tasks at random intervals
// - a task is a goroutine that runs for 2 seconds.
// - a task runs concurrently to other task
// - the interval between task is between 0 and 2 seconds

func main() {
// set x to the number of tasks
x := 4
// random numbers generation initialization
var wg sync.WaitGroup
random := rand.New(rand.NewSource(1234))

for num := 0; num < x; num++ {
// sleep for a random amount of milliseconds before starting a new task
duration := time.Millisecond * time.Duration(random.Intn(2000))
time.Sleep(duration)
//
wg.Add(1)
// run a task
go func() {
// this is the work, expressed by sleeping for 2 seconds
time.Sleep(2 * time.Second)
fmt.Println("task done")
wg.Done()
}()
}
wg.Wait()
fmt.Println("All tasks done")
}

输出:

task done
task done
task done
task done
All tasks done

Playground 上:https://play.golang.org/p/V-olyX9Qm8

使用 channel :

package main

import (
"fmt"
"math/rand"
"time"
)

// run x tasks at random intervals
// - a task is a goroutine that runs for 2 seconds.
// - a task runs concurrently to other task
// - the interval between task is between 0 and 2 seconds

func main() {
//Channel to indicate completion of a task, can be helpful in sending a result value also
results := make(chan int)
// set x to the number of tasks
x := 4
t := 0 //task tracker
// random numbers generation initialization
random := rand.New(rand.NewSource(1234))

for num := 0; num < x; num++ {
// sleep for a random amount of milliseconds before starting a new task
duration := time.Millisecond * time.Duration(random.Intn(2000))
time.Sleep(duration)
//

// run a task
go func() {
// this is the work, expressed by sleeping for 2 seconds
time.Sleep(2 * time.Second)
fmt.Println("task done")
results <- 1 //may be something possibly relevant to the task

}()
}
//Iterate over the channel till the number of tasks
for result := range results {
fmt.Println("Got result", result)
t++
if t == x {
close(results)
}
}
fmt.Println("All tasks done")
}

输出:

task done
task done
Got result 1
Got result 1
task done
Got result 1
task done
Got result 1
All tasks done

Playground :https://play.golang.org/p/yAFdDj5nhb

关于html - 代码提前结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com