go语言-用channel控制goroutine的退出

用channel控制goroutine的退出

本文简要介绍了,如何用channel控制goroutine的退出的基本方法

for-range主动停止goruitine

复制代码
package main

import (
	"fmt"
	"sync"
	"time"
)

/*
Go并发编程模型:主动停止goroutine
方法一:for-rang从channel上接收值,直到channel关闭
*/
var wg sync.WaitGroup

func work(ch chan int) {
	defer wg.Done()
	// for range关键字,将其使用在channel上时,会自动等待channel的动作一直到channel被关闭close
	// 也就是clase(sh)后,for range就会退出
	for i := range ch {
		fmt.Println(i)
	}
	fmt.Println("work exit")
}

func main() {

	wg.Add(1)
	var ch chan int
	ch = make(chan int)

	go work(ch)

	for i := 0; i < 3; i++ {
		time.Sleep(1 * time.Second)
		ch <- i
	}

	time.Sleep(3 * time.Second)
	for i := 4; i < 7; i++ {
		time.Sleep(1 * time.Second)
		ch <- i
	}

	// close(sh)来控制"work"协程中的for range的完成
	close(ch)
	wg.Wait()
}

后台定时任务

复制代码
	// 设定一个定时器,当定时器触发是就执行一次任务
	tricker := time.NewTicker(1 * time.Second)
	defer fmt.Println("tricker.Stop()")
	defer tricker.Stop()

	for {
		select {
		case <-stopCh:
			fmt.Println("do1 exit.")
			return
		//心跳,心跳一次就执行一次任务
		case <-tricker.C:
			time.Sleep(1 * time.Second)
			fmt.Println("do1 doing....")
		}
	}

使用stopCh控制goroutine退出

复制代码
package main

import (
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func do1(stopCh chan struct{}) {
	defer wg.Done()
	for {
		select {
		case <-stopCh:
			fmt.Println("go1 exit.")
			return
		default:
			time.Sleep(1 * time.Second)
			fmt.Println("go1 doing....")
		}
	}
}
func do2(stopCh chan struct{}) {
	defer wg.Done()
	for {
		select {
		case <-stopCh:
			fmt.Println("go2 exit.")
			return
		default:
			time.Sleep(1 * time.Second)
			fmt.Println("go2 doing....")
		}
	}
}

func main() {

	wg.Add(2)
	stopCh := make(chan struct{})
	go do1(stopCh)
	go do2(stopCh)

	time.Sleep(5 * time.Second)

	// 让一个goroutine退出
	stopCh <- struct{}{}

	time.Sleep(5 * time.Second)

	// 让另一个goroutine退出
	stopCh <- struct{}{}

	wg.Wait()

}

关闭channel来控制goroutine退出

复制代码
package main

import (
	"fmt"
	"sync"
	"time"
)

/*
多个通道都关闭才退出
利用select的一个特性,select不会在nil的通道上进行等待
*/
var wg sync.WaitGroup

func work(in, exit1, exit2 chan bool) {
	defer wg.Done()

	for {
		select {
		// 当exit管道收到信号后退出goroutine
		case v := <-exit1:
			fmt.Println("exit1 收到退出信号")
			fmt.Println("v=", v)
			exit1 = nil

		case <-exit2:
			fmt.Println("exit2 收到退出信号")
			exit2 = nil

		case value := <-in:
			fmt.Println(value)
		}
		fmt.Println(time.Now())
		fmt.Println("遍历了一次")

		// 当2个退出通道都收到信号时,就退出for循环
		if exit1 == nil && exit2 == nil {
			return
		}
	}

}

func main() {

	in := make(chan bool)
	exit1 := make(chan bool)
	exit2 := make(chan bool)

	wg.Add(1)
	go work(in, exit1, exit2)

	for i := 0; i < 6; i++ {
		time.Sleep(5 * time.Millisecond)
		in <- true
	}

	// 主动停止goroutine方法一
	//exit1 <- 1
	//exit2 <- 1

	// 主动停止goroutine方法二
	close(exit1)
	close(exit2)

	wg.Wait()

}
相关推荐
Hy行者勇哥15 分钟前
Python 与 VS Code 结合操作指南
开发语言·python
不羁。。3 小时前
【撸靶笔记】第七关:GET - Dump into outfile - String
数据库·笔记·oracle
yangchanghua1115 小时前
pgsql 如何查询今天范围内的数据(当天0点0分0秒 - 当天23点59分59秒....)
数据库·pgsql
larance5 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
python_chai5 小时前
从数据汇总到高级分析,SQL 查询进阶实战(下篇)—— 分组、子查询与窗口函数全攻略
数据库·sql·mysql
在努力的前端小白5 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
未来之窗软件服务5 小时前
自建知识库,向量数据库 (九)之 量化前奏分词服务——仙盟创梦IDE
数据库·仙盟创梦ide·东方仙盟·自建ai·ai分词
麦兜*6 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
萧鼎7 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
艾伦~耶格尔8 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试