当前位置:网站首页>2020-09-09:裸写算法:两个线程轮流打印数字1-100。
2020-09-09:裸写算法:两个线程轮流打印数字1-100。
2020-11-06 21:50:00 【福大大架构师每日一题】
福哥答案2020-09-09:
方法1:用一个通道,两个go程的代码不一样。
方法2:用两个通道,两个go程的代码完全一样。可以扩展成N个go程轮流打印。
代码用golang编写,代码如下:
package test38_alternateprint
import (
"fmt"
"testing"
"time"
)
var POOL = 10
//go test -v -test.run TestAlternatePrint
func TestAlternatePrint(t *testing.T) {
AlternatePrint1()
AlternatePrint2()
}
//方法1
func AlternatePrint1() {
fmt.Println("方法1,两个协程的代码不一样")
msg := make(chan int)
go func(p chan int) {
for i := 1; i <= POOL; i++ {
p <- i
if i%2 == 1 {
fmt.Println("groutine-1:", i)
}
}
}(msg)
go func(p chan int) {
for i := 1; i <= POOL; i++ {
<-p
if i%2 == 0 {
fmt.Println("groutine-2:", i)
fmt.Println("")
}
}
}(msg)
//等待协程执行完成
time.Sleep(time.Second * 1)
}
//方法2
func AlternatePrint2() {
fmt.Println("方法2,两个go程的代码完全一样")
const N = 2
chs := make([]chan struct{}, N)
for i := 0; i < N; i++ {
chs[i] = make(chan struct{}, 0)
}
start := 1
for i := 0; i < N; i++ {
go func(i int) {
for start <= POOL-N+1 {
//获得执行权
<-chs[i]
//执行代码
fmt.Printf("go程%d:%d\r\n", i, start)
if (i+1)%N == 0 {
fmt.Println("")
}
start++
//给其他协程执行权
chs[(i+1)%N] <- struct{}{}
}
}(i)
}
//给第1个协程执行权,第1个协程的序号是0
chs[0] <- struct{}{}
//等待协程执行完成
time.Sleep(time.Second * 1)
//收回最后1个go程的执行权
<-chs[POOL%N]
}
敲 go test -v -test.run TestAlternatePrint 命令,结果如下:
版权声明
本文为[福大大架构师每日一题]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4553401/blog/4555324
边栏推荐
- Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- From overseas to China, rancher wants to do research on container cloud market
- What is the tensor in tensorflow?
- C語言I部落格作業03
- Humor: hacker programming is actually similar to machine learning!
- IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
- It's time for your financial report to change to a more advanced style -- financial analysis cockpit
- electron 實現檔案下載管理器
- CloudQuery V1.2.0 版本发布
猜你喜欢
Gather in Beijing! The countdown to openi 2020
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
【:: 是什么语法?】
行为型模式之解释器模式
What is the tensor in tensorflow?
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
实用工具类函数(持续更新)
GUI engine evaluation index
What knowledge do Python automated testing learn?
Markdown tricks
随机推荐
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
[efficiency optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
行为型模式之备忘录模式
image operating system windows cannot be used on this platform
What is the tensor in tensorflow?
C語言I部落格作業03
An article taught you to use HTML5 SVG tags
小游戏云开发入门
Pn8162 20W PD fast charging chip, PD fast charging charger scheme
An article will take you to understand CSS3 fillet knowledge
How to play sortable JS vuedraggable to realize nested drag function of forms
Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
nacos、ribbon和feign的簡明教程
消息队列(MessageQueue)-分析
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
Elasticsearch Part 6: aggregate statistical query
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
Network programming NiO: Bio and NiO
Summary of front-end performance optimization that every front-end engineer should understand: