当前位置:网站首页>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)
}

边栏推荐
- 01-Golang-环境搭建
- 100 important knowledge points for SQL: in operator
- 100 important knowledge points that SQL must master: retrieving data
- MySQL速成——第一天--基础入门
- 体验Navicat Premium 16,无限重置试用14天方法(附源码)
- Love number experiment | Issue 7 - Financial Crisis Analysis Based on random forest
- 让马化腾失望了!Web3.0,毫无希望
- Share an experience of self positioning + problem solving
- 白嫖红队goby&POC,叫你如何白嫖?
- Shuttle hides the return button of the AppBar
猜你喜欢

Codeforces Global Round 14

一套系统,减轻人流集中地10倍的通行压力

GFS分布式文件系统

VMware vSphere ESXi 7.0安装教程

How to do a good job of gateway high availability protection in the big promotion scenario

VMware vSphere esxi 7.0 installation tutorial

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

White whoring red team goby & POC, how do you call white whoring?

行业案例|从零售之王看银行数字化转型的运营之道

Galaxy Kirin system LAN file sharing tutorial
随机推荐
SQL Server for循环用法
Tutorial | fNIRS data processing toolkit homer2 download and installation
TypeScript学习
GFS分布式文件系统
划重点!国产电脑上安装字体小技巧
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
PCIE知识点-008:PCIE switch的结构
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
After being forced to develop the app within 20 days, the group was laid off, and the technical director angrily criticized it: I wish "closure as soon as possible!"
Use the storcli tool to configure raid. Just collect this article
ABC-Teleporter Setting-(思维+最短路)
Go从入门到实战—— 多路选择和超时控制(笔记)
A set of system to reduce 10 times the traffic pressure in crowded areas
银河麒麟系统局域网文件共享教程
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
Kirin V10 installation font
gomock mockgen : unknown embedded interface
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Flexible IP network test tool -- x-launch
DO280OpenShift访问控制--security policy和章节实验