当前位置:网站首页>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
边栏推荐
- GitHub: the foundation of the front end
- Python basic variable type -- list analysis
- The legality of IPFs / filecoin: protecting personal privacy from disclosure
- list转换map(根据key来拆分list,相同key的value为一个list)
- 游戏主题音乐对游戏的作用
- C#和C/C++混合编程系列5-内存管理之GC协同
- 意外的元素..所需元素..
- 嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
- MongoDB与SQL常用语法对应表
- Gather in Beijing! The countdown to openi 2020
猜你喜欢

事件监听问题

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

游戏开发中的新手引导与事件管理系统

CCR coin frying robot: the boss of bitcoin digital currency, what you have to know

The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures

2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办

嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌

Get twice the result with half the effort: automation without cabinet

代码生成器插件与Creator预制体文件解析

ES6 learning notes (2): teach you to play with class inheritance and class objects
随机推荐
CloudQuery V1.2.0 版本发布
DC-1靶機
行为型模式之解释器模式
实用工具类函数(持续更新)
Xmppmini project details: step by step from the principle of practical XMPP technology development 4. String decoding secrets and message package
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
【應用程式見解 Application Insights】Application Insights 使用 Application Maps 構建請求鏈路檢視
hdu3974 Assign the task線段樹 dfs序
What are PLC Analog input and digital input
What are the common problems of DTU connection
IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
How about small and medium-sized enterprises choose shared office?
Tron smart wallet PHP development kit [zero TRX collection]
JNI-Thread中start方法的呼叫與run方法的回撥分析
使用 Iceberg on Kubernetes 打造新一代雲原生資料湖
ES中删除索引的mapping字段时应该考虑的点
MongoDB与SQL常用语法对应表
Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
開源一套極簡的前後端分離專案腳手架