当前位置:网站首页>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
边栏推荐
- 代码重构之法——方法重构分析
- Details of dapr implementing distributed stateful service
- Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
- Summary of front-end performance optimization that every front-end engineer should understand:
- 如何在终端启动Coda 2中隐藏的首选项?
- Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
- mongo 用户权限 登录指令
- Python basic variable type -- list analysis
- Helping financial technology innovation and development, atfx is at the forefront of the industry
- 【转发】查看lua中userdata的方法
猜你喜欢

EOS founder BM: what's the difference between UE, UBI and URI?

image operating system windows cannot be used on this platform

Helping financial technology innovation and development, atfx is at the forefront of the industry

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

How to turn data into assets? Attracting data scientists

解决 WPF 绑定集合后数据变动界面却不更新的问题

Vue communication and cross component listening state Vue communication

小游戏云开发入门

What is alicloud's experience of sweeping goods for 100 yuan?

Pn8162 20W PD fast charging chip, PD fast charging charger scheme
随机推荐
How does filecoin's economic model and future value support the price of fil currency breaking through thousands
Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
如何对数据库账号权限进行精细化管理?
Analysis of serilog source code -- how to use it
小游戏云开发入门
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
A small goal in 2019 to become a blog expert of CSDN
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
Tron smart wallet PHP development kit [zero TRX collection]
Xmppmini project details: step by step from the principle of practical XMPP technology development 4. String decoding secrets and message package
ES6 learning notes (4): easy to understand the new grammar of ES6
list转换map(根据key来拆分list,相同key的value为一个list)
StickEngine-架构12-通信协议
Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
視覺滾動[反差美]
What is the purchasing supplier system? Solution of purchasing supplier management platform
image operating system windows cannot be used on this platform
Helping financial technology innovation and development, atfx is at the forefront of the industry