当前位置:网站首页>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
边栏推荐
- To Lianyun analysis: why is IPFs / filecoin mining so difficult?
- 事务的隔离级别与所带来的问题
- Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
- Git rebase is in trouble. What to do? Waiting line
- An article will take you to understand CSS3 fillet knowledge
- 游戏主题音乐对游戏的作用
- 统计项目代码行数
- C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
- 实用工具类函数(持续更新)
- jenkins安装部署过程简记
猜你喜欢

如何在终端启动Coda 2中隐藏的首选项?

Contract trading system development | construction of smart contract trading platform

Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?

消息队列(MessageQueue)-分析

An article takes you to understand CSS pagination examples

Diamond standard

MongoDB与SQL常用语法对应表

How to turn data into assets? Attracting data scientists

list转换map(根据key来拆分list,相同key的value为一个list)

Swagger 3.0 brushes the screen every day. Does it really smell good?
随机推荐
ES6 learning notes (2): teach you to play with class inheritance and class objects
游戏开发中的新手引导与事件管理系统
Diamond standard
How does filecoin's economic model and future value support the price of fil currency breaking through thousands
事件监听问题
行为型模式之备忘录模式
【ElasticSearch搜索引擎】
Multi robot market share solution
Use modelarts quickly, zero base white can also play AI!
Ronglian completed US $125 million f round financing
Helping financial technology innovation and development, atfx is at the forefront of the industry
MongoDB与SQL常用语法对应表
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
Zero basis to build a web search engine of its own
Tron smart wallet PHP development kit [zero TRX collection]
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
What are PLC Analog input and digital input
Python basic data type -- tuple analysis
Take you to learn the new methods in Es5
How to play sortable JS vuedraggable to realize nested drag function of forms