当前位置:网站首页>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
边栏推荐
- 统计项目代码行数
- 前端未來趨勢之原生API:Web Components
- 解决 WPF 绑定集合后数据变动界面却不更新的问题
- 視覺滾動[反差美]
- Junit测试出现 empty test suite
- Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
- StickEngine-架构12-通信协议
- Application of restful API based on MVC
- To Lianyun analysis: why is IPFs / filecoin mining so difficult?
- 游戏主题音乐对游戏的作用
猜你喜欢

代码重构之法——方法重构分析

An article takes you to understand CSS gradient knowledge

Diamond standard

Git rebase is in trouble. What to do? Waiting line

美团内部讲座|周烜:华东师范大学的数据库系统研究

Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom

Description of phpshe SMS plug-in

How to hide part of barcode text in barcode generation software

【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!

如何对数据库账号权限进行精细化管理?
随机推荐
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
【自学unity2d传奇游戏开发】如何让角色动起来
事务的隔离级别与所带来的问题
html+vue.js 實現分頁可相容IE
ES6 learning notes (2): teach you to play with class inheritance and class objects
Gather in Beijing! The countdown to openi 2020
git远程库回退指定版本
行为型模式之解释器模式
Vue communication and cross component listening state Vue communication
hdu3974 Assign the task線段樹 dfs序
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
Swagger 3.0 brushes the screen every day. Does it really smell good?
image operating system windows cannot be used on this platform
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
【学习】接口测试用例编写和测试关注点
Introduction to Google software testing
GUI engine evaluation index
Zero basis to build a web search engine of its own
An article will introduce you to HTML tables and their main attributes