当前位置:网站首页>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
边栏推荐
- 2020年数据库技术大会助力技术提升
- Swagger 3.0 brushes the screen every day. Does it really smell good?
- What are PLC Analog input and digital input
- Basic usage of GDB debugging
- Multi robot market share solution
- 检测证书过期脚本
- Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
- 【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
- An article will introduce you to HTML tables and their main attributes
- 嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
猜你喜欢
How to hide part of barcode text in barcode generation software
Zero basis to build a web search engine of its own
What is alicloud's experience of sweeping goods for 100 yuan?
ES中删除索引的mapping字段时应该考虑的点
MongoDB与SQL常用语法对应表
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
From overseas to China, rancher wants to do research on container cloud market
StickEngine-架构11-消息队列(MessageQueue)
ERD-ONLINE 免费在线数据库建模工具
随机推荐
MongoDB与SQL常用语法对应表
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
An article taught you to use HTML5 SVG tags
检测证书过期脚本
Small program introduction to proficient (2): understand the four important files of small program development
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
游戏开发中的新手引导与事件管理系统
Contract trading system development | construction of smart contract trading platform
An article will take you to understand SVG gradient knowledge
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
What knowledge do Python automated testing learn?
Some operations kept in mind by the front end foundation GitHub warehouse management
Flink's datasource Trilogy 2: built in connector
Ronglian completed US $125 million f round financing
代码重构之法——方法重构分析
An article takes you to understand CSS gradient knowledge
使用 Iceberg on Kubernetes 打造新一代雲原生資料湖
How to understand Python iterators and generators?