golang代码 控制goroutine结束

    通过关闭channel来广播消息
    waitgroup的目的,是在main关闭channel后,给goroutine一些时间,完成某些操作

    func worker(wg *sync.WaitGroup, cannel chan bool) {
        defer wg.Done()

        for {
            select {
            default:
                fmt.Println("hello")
            case <-cannel:
                return
            }
        }
    }

    func main() {
        cancel := make(chan bool)

        var wg sync.WaitGroup
        for i := 0; i < 10; i++ {
            wg.Add(1)
            go worker(&wg, cancel)
        }

        time.Sleep(time.Second)
        close(cancel)
        wg.Wait()
    }

    --------------------------------------------------------------------------------
    context
    func worker(ctx context.Context, wg *sync.WaitGroup) error {
        defer wg.Done()

        for {
        select {
    default:
        fmt.Println("hello")
    case <-ctx.Done():
        return ctx.Err()
    }
    }
    }

    func main() {
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

        var wg sync.WaitGroup
        for i := 0; i < 10; i++ {
            wg.Add(1)
            go worker(ctx, &wg)
        }

        time.Sleep(time.Second)
        cancel()

        wg.Wait()
    }

猜你喜欢

转载自www.cnblogs.com/ts65214/p/12976313.html