当前位置:网站首页>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
边栏推荐
- Pn8162 20W PD fast charging chip, PD fast charging charger scheme
- Multi robot market share solution
- PHP application docking justswap special development kit【 JustSwap.PHP ]
- nacos、ribbon和feign的簡明教程
- 每个大火的“线上狼人杀”平台,都离不开这个新功能
- DC-1靶機
- C語言I部落格作業03
- Contract trading system development | construction of smart contract trading platform
- 【自学unity2d传奇游戏开发】地图编辑器
- What are the criteria for selecting a cluster server?
猜你喜欢
What are manufacturing and new automation technologies?
From overseas to China, rancher wants to do research on container cloud market
【学习】接口测试用例编写和测试关注点
An article takes you to understand CSS gradient knowledge
如何在终端启动Coda 2中隐藏的首选项?
C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
mongo 用户权限 登录指令
What are the criteria for selecting a cluster server?
Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
Contract trading system development | construction of smart contract trading platform
随机推荐
实用工具类函数(持续更新)
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
2020年数据库技术大会助力技术提升
Chainlink brings us election results into blockchain everipedia
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
What knowledge do Python automated testing learn?
Application of restful API based on MVC
An article will introduce you to CSS3 background knowledge
Vue communication and cross component listening state Vue communication
【自学unity2d传奇游戏开发】如何让角色动起来
Asp.Net Core learning notes: Introduction
Zero basis to build a web search engine of its own
如何在终端启动Coda 2中隐藏的首选项?
window系统 本机查找端口号占用方法
Gather in Beijing! The countdown to openi 2020
Pn8162 20W PD fast charging chip, PD fast charging charger scheme
A small goal in 2019 to become a blog expert of CSDN
游戏开发中的新手引导与事件管理系统
Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform