当前位置:网站首页>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
边栏推荐
- It's time for your financial report to change to a more advanced style -- financial analysis cockpit
- Markdown tricks
- (2) ASP.NET Core3.1 Ocelot routing
- ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
- 【自学unity2d传奇游戏开发】如何让角色动起来
- [efficiency optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
- C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
- EOS founder BM: what's the difference between UE, UBI and URI?
- 每个大火的“线上狼人杀”平台,都离不开这个新功能
- Introduction to the structure of PDF417 bar code system
猜你喜欢
mongo 用户权限 登录指令
Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
检测证书过期脚本
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
递归、回溯算法常用数学基础公式
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
Markdown tricks
ES6 learning notes (5): easy to understand ES6's built-in extension objects
Diamond standard
Multi robot market share solution
随机推荐
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
What is the purchasing supplier system? Solution of purchasing supplier management platform
(2) ASP.NET Core3.1 Ocelot routing
Python basic data type -- tuple analysis
How about small and medium-sized enterprises choose shared office?
mongo 用户权限 登录指令
CloudQuery V1.2.0 版本发布
实用工具类函数(持续更新)
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
Flink's datasource Trilogy 2: built in connector
StickEngine-架构11-消息队列(MessageQueue)
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
electron 實現檔案下載管理器
With this artifact, quickly say goodbye to spam messages
Zero basis to build a web search engine of its own
What are Devops
Building a new generation cloud native data lake with iceberg on kubernetes