Context多路复用和任务取消

当我们任务用到子任务时候,如果主任务关闭旗下所有子任务同时关闭那么就要用到Context多路复用 。
一。普通任务的取消
package cancel_by

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

func isCancelled(cancelChan chan struct{}) bool {
//多路选择,当case处于阻塞情况执行default
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, cancelChan chan struct{}) {
for {
if isCancelled(cancelChan) {
break
}
time.Sleep(time.Millisecond * 5)
}
fmt.Println(i, "Cancelled")
}(i, cancelChan)

}
cancel_2(cancelChan)
time.Sleep(time.Second * 1)
}

二。Context多路复用任务取消(根节点任务取消时候同时取消旗下子任务)
1.根Context:通过context.Background()创建
2.子Context:context.WithCancel(parentContext)创建
3. ctx,cancel:=context.WithCancel(context.Background())
4.当前Context被取消时,基于他的子context都会取消
5.接收取消通知<-ctx.Done()


package cancel_by

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

func isCancelledt(ctx context.Context) bool {
//多路选择,当case处于阻塞情况执行default
select {
case <-ctx.Done(): //是否正常收到消息
return true //通道正常
default:
return false //通道阻塞
}
}

//关联任务
func TestCancelt(t *testing.T) {
ctx, cancelt := context.WithCancel(context.Background()) //父节点
for i := 0; i < 5; i++ {
go func(i int, ctx context.Context) {
for {
if isCancelledt(ctx) {
break
}
time.Sleep(time.Millisecond * 5)
}
fmt.Println(i, "Fk")
}(i, ctx)//ctx子任务

}
cancelt() //取消当前执行cancel的包裹所有子任务
time.Sleep(time.Second * 1)
}

 

 

posted @ 2019-07-10 15:42  易云客  阅读(943)  评论(0编辑  收藏  举报