在工作和学习总,使用Golang的实现业务逻辑的时候,往往需要使流程阻塞一段时间等待其他协程的执行;或者永久阻塞来监听一些连接信息等。下面提供了几种常见的阻塞方式,仅供参考。
sync.WaitGroup
是 Golang 中常用的并发措施,我们可以用它来等待一批 Goroutine 结束。
func chokeWithWaitGroup() {
start := time.Now()
wg := sync.WaitGroup{}
// 增加阻塞计数器
wg.Add(1)
go func() {
time.Sleep(chokeTime)
// 扣减阻塞计数器
wg.Done()
}()
// 等待阻塞计数器到 0
wg.Wait()
d := time.Since(start)
fmt.Println("使用WaitGroup阻塞了:", d)
}
select
是 Go 中的一个控制结构,类似于 switch 语句。
func chokeWithSelect() {
start := time.Now()
// 启动定时器
timer := time.NewTimer(chokeTime)
select {
case <-timer.C:
d := time.Since(start)
fmt.Println("使用Select阻塞了:", d)
}
}
Go 语言中的通道channel
是一种特殊的类型。
通道像一个传送带或者队列,总是遵循先入先出(First In First Out)的规则,保证收发数据的顺序。
当没有给channel设置空间的时候,称为无缓冲通道或者阻塞通道或者同步通道。
func chokeWithChannel() {
start := time.Now()
var ch = make(chan struct{})
go func() {
time.Sleep(chokeTime)
ch <- struct{}{}
}()
// 阻塞,等待channel数据
if _, ok := <-ch; ok {
d := time.Since(start)
fmt.Println("使用channel阻塞了:", d)
}
}
Go 语言中的time包提供了时间的显示和测量用的函数。其中time.After
会在一段时间后返回一个时间信号。且是一次性的定时。
因此,可以通过time.After来阻塞固定时长。
func chokeWithTimeAfter() {
start := time.Now()
<-time.After(chokeTime)
d := time.Since(start)
fmt.Println("使用channel阻塞了:", d)
}
使用for{}
循环来控制阻塞,这个不需要解释了。但是会一直占用CPU的资源,慎用。
func chokeWithFor() {
start := time.Now()
for {
d := time.Since(start)
if d > chokeTime {
fmt.Println("使用For阻塞了:", d)
return
}
}
}
使用mutex
锁的时候,如果多个协程同时想要获取锁,那么就会出现竞争关系。当第二个协程在尝试获取锁的时候,发现锁已经被其他协程占用,则会一直不停的尝试获取锁,形成阻塞。
func chokeWithMutex() {
start := time.Now()
mu := sync.Mutex{}
mu.Lock()
go func() {
mu.Lock()
defer mu.Unlock()
d := time.Since(start)
fmt.Println("使用mutex阻塞了:", d)
}()
time.Sleep(chokeTime)
mu.Unlock()
time.Sleep(chokeTime)
}
goto
是调整代码执行位置,如果形成了一个循环的化,同样会阻塞程序。
func chokeWithGoto() {
start := time.Now()
here:
d := time.Since(start)
if d > chokeTime {
fmt.Println("使用Goto阻塞了:", d)
return
}
goto here
}
package choke
import (
"fmt"
"sync"
"time"
)
var chokeTime = 1 * time.Second
// chokeWithWaitGroup 使用WaitGroup()控制阻塞
func chokeWithWaitGroup() {
start := time.Now()
wg := sync.WaitGroup{}
// 增加阻塞计数器
wg.Add(1)
go func() {
time.Sleep(chokeTime)
// 扣减阻塞计数器
wg.Done()
}()
// 等待阻塞计数器到 0
wg.Wait()
d := time.Since(start)
fmt.Println("使用WaitGroup阻塞了:", d)
}
// chokeWithSelect 使用select控制阻塞
func chokeWithSelect() {
start := time.Now()
// 启动定时器
timer := time.NewTimer(chokeTime)
select {
case <-timer.C:
d := time.Since(start)
fmt.Println("使用Select阻塞了:", d)
}
}
// chokeWithFor 使用for控制阻塞
func chokeWithFor() {
start := time.Now()
for {
d := time.Since(start)
if d > chokeTime {
fmt.Println("使用For阻塞了:", d)
return
}
}
}
// chokeWithMutex 使用mutex控制阻塞
func chokeWithMutex() {
start := time.Now()
mu := sync.Mutex{}
mu.Lock()
go func() {
mu.Lock()
defer mu.Unlock()
d := time.Since(start)
fmt.Println("使用mutex阻塞了:", d)
}()
time.Sleep(chokeTime)
mu.Unlock()
time.Sleep(chokeTime)
}
// chokeWithChannel 使用channel控制阻塞
func chokeWithChannel() {
start := time.Now()
var ch = make(chan struct{})
go func() {
time.Sleep(chokeTime)
ch <- struct{}{}
}()
// 阻塞,等待channel数据
if _, ok := <-ch; ok {
d := time.Since(start)
fmt.Println("使用channel阻塞了:", d)
}
}
// chokeWithTimeAfter 使用time.After控制阻塞
func chokeWithTimeAfter() {
start := time.Now()
<-time.After(chokeTime)
d := time.Since(start)
fmt.Println("使用channel阻塞了:", d)
}
// chokeWithGoto 使用goto控制阻塞
func chokeWithGoto() {
start := time.Now()
here:
d := time.Since(start)
if d > chokeTime {
fmt.Println("使用Goto阻塞了:", d)
return
}
goto here
}
package choke
import "testing"
func TestChoke(t *testing.T) {
chokeWithWaitGroup()
chokeWithSelect()
chokeWithChannel()
chokeWithTimeAfter()
chokeWithFor()
chokeWithMutex()
chokeWithGoto()
}
=== RUN TestChoke
使用WaitGroup阻塞了: 1.000691546s
使用Select阻塞了: 1.001008044s
使用For阻塞了: 1.000650866s
使用mutex阻塞了: 1.000067636s
使用channel阻塞了: 1.00013111s
使用channel阻塞了: 1.000485065s
使用Goto阻塞了: 1.000000004s
--- PASS: TestChoke (8.00s)
PASS
Process finished with the exit code 0
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。