用channel实现goroutine交替打印最常用,核心是两个chan struct{}控制执行权:A打印后发信号给chB唤醒B,B打印完再发信号给chA;需初始化chA为带缓冲chan以避免死锁。

用 channel 实现 goroutine 交替打印(最常用)
这是最直观、符合 Go 并发模型的设计。核心是用两个 chan struct{} 控制执行权流转:goroutine A 打印后往 chB 发信号,唤醒 B;B 打印完再发信号给 chA。注意初始必须有一个 goroutine 先拿到“令牌”,否则会死锁。
chA := make(chan struct{}, 1)
chB := make(chan struct{})
chA <- struct{}{} // A 先跑
go func() {
for i := 1; i <= 100; i += 2 {
<-chA
fmt.Println(i)
chB <- struct{}{}
}
}()
go func() {
for i := 2; i <= 100; i += 2 {
<-chB
fmt.Println(i)
chA <- struct{}{}
}
}()
go func() { for i := 1; i <= 100; i += 2 { <-chA fmt.Println(i) chB <- struct{}{} } }() go func() { for i := 2; i <= 100; i += 2 { <-chB fmt.Println(i) chA <- struct{}{} } }()
常见错误:缓冲区设为 0 且没预置信号 → 两个 goroutine 都在等对方,直接 deadlock。别漏掉 chA 这一行。
用 sync.Mutex + sync.Cond 实现交替(适合理解条件等待)sync.Cond 是为“等待某个条件成立”而生的,比纯 sync.Mutex 更精准。这里用一个 sync.Mutex 保护共享变量 turn(标识该谁打印),再用两个 sync.Cond 分别通知 A 和 B。
var mu sync.Mutex
condA := sync.NewCond(&mu)
condB := sync.NewCond(&mu)
turn := 0 // 0: A, 1: B
go func() {
for i := 1; i <= 100; i += 2 {
mu.Lock()
for turn != 0 {
condA.Wait()
}
fmt.Println(i)
turn = 1
condB.Signal()
mu.Unlock()
}
}()
go func() { for i := 1; i <= 100; i += 2 { mu.Lock() for turn != 0 { condA.Wait() } fmt.Println(i) turn = 1 condB.Signal() mu.Unlock() } }()
注意点:必须在 Lock() 后用 for 循环检查条件(不是 if),防止虚假唤醒;Signal() 不保证立刻唤醒,但这里只有两个 goroutine,够用。
用 atomic.Int32 做无锁轮转(轻量但需小心边界)
如果只是交替,不涉及复杂状态,atomic.Int32 足够。定义一个原子计数器,值为 0 表示轮到 A,1 表示轮到 B。每个 goroutine 自旋检查,匹配则打印并切换。
var turn atomic.Int32
turn.Store(0)
go func() {
for i := 1; i <= 100; i += 2 {
for turn.Load() != 0 {
runtime.Gosched() // 主动让出,避免忙等耗 CPU
}
fmt.Println(i)
turn.Store(1)
}
}()
go func() { for i := 1; i <= 100; i += 2 { for turn.Load() != 0 { runtime.Gosched() // 主动让出,避免忙等耗 CPU } fmt.Println(i) turn.Store(1) } }()
性能上比 channel 或 mutex 略高,但要注意:
- 忙等(busy-wait)不加
runtime.Gosched()会吃满一个 CPU 核; -
atomic不能替代锁来保护多字段状态,这里只管单个整数,没问题。
用 select + time.After 实现带超时的交替(防卡死)
纯 channel 方案一旦某 goroutine panic 或提前退出,另一个会永久阻塞在 `select 和 time.After 可做兜底,适用于对稳定性要求高的场景。
chA := make(chan struct{}, 1)
chB := make(chan struct{})
chA <- struct{}{}
go func() {
for i := 1; i <= 100; i += 2 {
select {
case <-chA:
fmt.Println(i)
chB <- struct{}{}
case <-time.After(3 * time.Second):
fmt.Println("A timeout, exiting")
return
}
}
}()
go func() { for i := 1; i <= 100; i += 2 { select { case <-chA: fmt.Println(i) chB <- struct{}{} case <-time.After(3 * time.Second): fmt.Println("A timeout, exiting") return } } }()
适用场景有限——正常逻辑不该超时,但它能帮你快速发现哪个 goroutine 挂了。别滥用,会掩盖真实问题。
用 context.WithCancel 实现可中断的交替(适合长任务)
当打印过程可能被外部取消(比如用户按 Ctrl+C),用 context.Context 最自然。每个 goroutine 监听 ctx.Done(),一收到就退出。
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
defer cancel() // A 出错或完成,主动 cancel 整体
for i := 1; i <= 100; i += 2 {
select {
case <-chA:
fmt.Println(i)
chB <- struct{}{}
case <-ctx.Done():
return
}
}
}()
go func() { defer cancel() // A 出错或完成,主动 cancel 整体 for i := 1; i <= 100; i += 2 { select { case <-chA: fmt.Println(i) chB <- struct{}{} case <-ctx.Done(): return } } }()
关键点:cancel 调用要放在 defer 或明确位置,否则可能泄漏;两个 goroutine 都监听同一个 ctx,确保同步退出。
交替逻辑本身简单,但每种写法背后对应不同工程诉求:channel 通用,Cond 适合教学,atomic 要防忙等,select+timeout 是防御性编程,context 是面向生命周期管理。选哪种,取决于你真正想解决的问题,而不是“看起来酷不酷”。











