当前位置:网站首页>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)
}
边栏推荐
- Full record of 2022 open source moment at Huawei partners and Developers Conference
- Go从入门到实战——所有任务完成(笔记)
- 关于企业数字化的展望(38/100)
- 快递e栈——数组篇小型项目
- SQL必需掌握的100个重要知识点:组合 WHERE 子句
- 100 important knowledge points that SQL must master: combining where clauses
- 专题教程——选队长游戏
- 流程控制任务
- SQL必需掌握的100个重要知识点:排序检索数据
- Prospects for enterprise digitalization (38/100)
猜你喜欢
AI painting minimalist tutorial
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
Galaxy Kirin system LAN file sharing tutorial
Codeforces Round #723 (Div. 2)
Flood fighting and disaster relief, overcoming difficulties, and City United premium products rushed to the aid of Yingde to donate loving materials
Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!
释放开源数据库创新力量 | 【甘肃】openGauss Meetup圆满结束
Tiktok's interest in e-commerce has hit the traffic ceiling?
Codeforces Round #716 (Div. 2)
随机推荐
Go从入门到实战——多态(笔记)
TreeSet详解
Wechat applet based service management system for college party members' Home System applet graduation design, Party members, activists, learning, punch in, forum
oss上传调用的是哪个方法
squid代理服務器
GoLand永久激活
SQL必需掌握的100个重要知识点:用通配符进行过滤
SQL server for circular usage
String类的常用方法
SQL Server for循环用法
SQL必需掌握的100个重要知识点:排序检索数据
Share how I take notes
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
Love math experiment | Issue 8 - building of Singapore house price prediction model
Codeforces Round #723 (Div. 2)
华为伙伴暨开发者大会2022开源时刻全纪录
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
SQL必需掌握的100个重要知识点:IN 操作符
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
事件相关电位ERP的皮层溯源分析