当前位置:网站首页>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
边栏推荐
- 面试官: ShardingSphere 学一下吧
- Small program introduction to proficient (2): understand the four important files of small program development
- The legality of IPFs / filecoin: protecting personal privacy from disclosure
- 如何对数据库账号权限进行精细化管理?
- Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
- Asp.Net Core learning notes: Introduction
- window系统 本机查找端口号占用方法
- Share with Lianyun: is IPFs / filecoin worth investing in?
- Git rebase is in trouble. What to do? Waiting line
- Get twice the result with half the effort: automation without cabinet
猜你喜欢
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
An article taught you to use HTML5 SVG tags
Gather in Beijing! The countdown to openi 2020
Zero basis to build a web search engine of its own
谷歌浏览器实现视频播放加速功能
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
A small goal in 2019 to become a blog expert of CSDN
代码生成器插件与Creator预制体文件解析
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
随机推荐
每个大火的“线上狼人杀”平台,都离不开这个新功能
EOS founder BM: what's the difference between UE, UBI and URI?
Python basic variable type -- list analysis
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
Chainlink brings us election results into blockchain everipedia
JNI-Thread中start方法的呼叫與run方法的回撥分析
Xmppmini project details: step by step from the principle of practical XMPP technology development 4. String decoding secrets and message package
Outsourcing is really difficult. As an outsourcer, I can't help sighing.
Humor: hacker programming is actually similar to machine learning!
消息队列(MessageQueue)-分析
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
What is alicloud's experience of sweeping goods for 100 yuan?
Git rebase is in trouble. What to do? Waiting line
Gather in Beijing! The countdown to openi 2020
The method of realizing high SLO on large scale kubernetes cluster
Filecoin has completed a major upgrade and achieved four major project progress!
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
From overseas to China, rancher wants to do research on container cloud market
CloudQuery V1.2.0 版本发布