Comments (1)
交替打印数字和字母
问题描述
使用两个 goroutine
交替打印序列,一个 goroutine
打印数字, 另外一个 goroutine
打印字母, 最终效果如下:
12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
考点
对 goroutine
状态控制,执行顺序控制, 对 chan
的应用, 以及正确的退出程序。
解题思路
问题很简单,使用 channel 来控制打印的进度。使用两个 channel ,来分别控制数字和字母的打印序列, 数字打印完成后通过 channel 通知字母打印, 字母打印完成后通知数字打印,然后周而复始的工作。
源码参考
package main
import (
"fmt"
"sync"
)
func main() {
letter, number := make(chan bool), make(chan bool)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
i := 1
for {
next := <-number
for j := 0; j < 2; j++ {
fmt.Print(i)
i++
}
if next {
letter <- true
continue
}
break
}
close(letter)
}()
go func() {
defer wg.Done()
l := 'A'
for l <= 'Z' {
<-letter
for i := 0; i < 2 && l <= 'Z'; i++ {
fmt.Print(string(l))
l++
}
number <- l <= 'Z'
}
close(number)
}()
number <- true
wg.Wait()
}
源码解析
- 使用
letter
和number
两个chan
控制goroutine
执行顺序,letter
控制打印字母,number
控制打印数字, 倒数第三行通知优先打印数字。 letter
停止打印是字母表控制的, 当打印完Z
字母之后即终止,number
需要接受是否终止(退出)的事件,这个事件是由打印字母的goroutine
控制的, 所以使用chan bool
类型 , 为了保持一致性letter
并没有采用chan struct{}
类型,而是和number
一致的chan bool
。- 字母采用字节码计算, 可以很好的完成自增和边界检查。
- 当字母打印完
Z
之母之后,会向number
发送false
, 用来表示letter
不在接收新的任务,并且打印任务也将结束, 并且关闭number
通道。 number
接收到打印任务事件后,先打印数字,之后检查接收的next
是否为true
, 如果是true
则通知letter
进行字母打印, 否者的话就退出循环,并且关闭letter
通道。- 两个
goroutine
退出,wg.Wait() 结束,退出main
函数,整个程序结束。
重点
- 程序中使用的
chan
都是在写端关闭的,这是chan
安全使用的最基本准则 - 程序退出前正确的关闭所有的
chan
和goroutine
相关资源
from interview-go.
Related Issues (20)
- 实现 Sunday 匹配 精简版代码
- 第三题的答案是否使用了额外的空间 HOT 1
- gpm窃取过程不对 HOT 1
- 常见语法题目 一 的第6题,CAS操作 HOT 3
- 一些建议 HOT 4
- 语法找错题 最后一道解答错误
- question17/多协程切片查询
- 最长公共前缀 go版本代码有误
- 第六题机器人坐标计算有问题
- 冒泡排序完全是错的 HOT 1
- “实现阻塞读且并发安全的map”答案补充 HOT 1
- q001中的chan类型应该用struct{}
- 实现 Sunday 匹配 needle在最末尾的时候返回结果异常
- 读阻塞且并发安全的 Map,答案是否有问题?
- 最接近的三数之和,代码有误
- Goroutine泄露问题——交替打印数字和字母
- q006 机器人坐标,L 跟 R指令是不是调转了
- 判断字符串中字符是否全都不同为什么有个127的判断?
- 『goroutine和channel使用一』 答案协程泄露
- interview-go/question /q015.md
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interview-go.