当前位置:网站首页>Go from introduction to actual combat - context and task cancellation (notes)
Go from introduction to actual combat - context and task cancellation (notes)
2022-06-27 21:37:00 【Accumulated Ytu】
Cancellation of associated tasks

Context

func isCancelled(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
func TestCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
for i := 0; i < 5; i++ {
go func(i int, ctx context.Context) {
for {
if isCancelled(ctx) {
break
}
time.Sleep(time.Millisecond * 5)
}
fmt.Println(i, "Done")
}(i, ctx)
}
cancel()
time.Sleep(time.Second * 1)
}

边栏推荐
- Go从入门到实战——Context与任务取消(笔记)
- Love math experiment | phase VI - Financial anti fraud case study
- AI 绘画极简教程
- Go从入门到实战——channel的关闭和广播(笔记)
- BTC and eth recapture the lost land! Leading the market recovery? Encryption will enter the "ice age"!
- Shuttle hides the return button of the AppBar
- Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
- SQL必需掌握的100个重要知识点:创建计算字段
- VMware vSphere esxi 7.0 installation tutorial
- Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
猜你喜欢

Codeforces Round #719 (Div. 3)

Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials

Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum

Codeforces Round #716 (Div. 2)

SQL必需掌握的100个重要知识点:过滤数据

非常全面的DolphinScheduler(海豚调度)安装使用文档

MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)

CORBA 架构体系指南(通用对象请求代理体系架构)

体验Navicat Premium 16,无限重置试用14天方法(附源码)

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China
随机推荐
Acwing周赛57-数字操作-(思维+分解质因数)
Is it safe to open an account and buy stocks? Who knows
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
Focus! Tips for installing fonts on domestic computers
行业案例|从零售之王看银行数字化转型的运营之道
Golang 使用正则来匹配出子字符串函数
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
100 important knowledge points that SQL must master: using functions to process data
OpenSSL 编程 二:搭建 CA
Educational Codeforces Round 108 (Rated for Div. 2)
富文本 考试 填空题
MySQL client tools are recommended. I can't imagine that it is best to use Juran
SQL必需掌握的100个重要知识点:创建计算字段
Go从入门到实战——Context与任务取消(笔记)
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
Ceph分布式存储
Go从入门到实战——所有任务完成(笔记)
华为伙伴暨开发者大会2022开源时刻全纪录
shell脚本控制服务的启动和关闭 - 具备详细案例