当前位置:网站首页>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
边栏推荐
- An article takes you to understand CSS3 picture border
- C#和C/C++混合编程系列5-内存管理之GC协同
- Description of phpshe SMS plug-in
- What are the criteria for selecting a cluster server?
- 华为云微认证考试简介
- Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
- Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
- 行为型模式之解释器模式
- ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录
- 常用SQL语句总结
猜你喜欢

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

Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?

前端未來趨勢之原生API:Web Components

每个大火的“线上狼人杀”平台,都离不开这个新功能

Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)

Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes

The method of realizing high SLO on large scale kubernetes cluster

游戏主题音乐对游戏的作用

Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios

Take you to learn the new methods in Es5
随机推荐
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
python100例項
An article takes you to understand CSS pagination examples
Basic usage of GDB debugging
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
ES中删除索引的mapping字段时应该考虑的点
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
An article will introduce you to HTML tables and their main attributes
The method of realizing high SLO on large scale kubernetes cluster
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
How to hide part of barcode text in barcode generation software
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
ado.net和asp.net的关系
Contract trading system development | construction of smart contract trading platform
(2) ASP.NET Core3.1 Ocelot routing
ERD-ONLINE 免费在线数据库建模工具
Zero basis to build a web search engine of its own
An article takes you to understand CSS gradient knowledge