Go语言学习笔记-并发编程-任务的取消

package concurrency

import (
    "fmt"
    "testing"
    "time"
)

func isCancelled(cancelChan chan struct{}) bool {
    select {
    case <-cancelChan:
        return true
    default:
        return false
    }
}

func cancel_1(cancelChan chan struct{}) {
    cancelChan <- struct{}{}
}

func cancel_2(cancelChan chan struct{}) {
    close(cancelChan)
}

func TestCancel(t *testing.T) {
    cancelChan := make(chan struct{}, 0)
    for i := 0; i < 5; i++ {
        go func(i int, cancelCh chan struct{}) {
            for {
                if isCancelled(cancelCh) {
                    break
                }
                time.Sleep(time.Millisecond * 5)
            }
            fmt.Println(i, "Cancelled")
        }(i, cancelChan)
    }
    cancel_2(cancelChan)
    time.Sleep(time.Second * 1)
}

你可能感兴趣的:(Go语言学习笔记-并发编程-任务的取消)