当前位置:网站首页>September 9, 2020: naked writing algorithm: two threads print numbers 1-100 in turn.
September 9, 2020: naked writing algorithm: two threads print numbers 1-100 in turn.
2020-11-06 21:50:00 【Fuda Dajia architect's daily question】
Fogo's answer 2020-09-09:
Method 1: With a channel , Two go The program code is different .
Method 2: Two channels , Two go The program code is exactly the same . It can be extended to N individual go Cheng takes turns printing .
The code to use golang To write , The code is as follows :
package test38_alternateprint
import (
"fmt"
"testing"
"time"
)
var POOL = 10
//go test -v -test.run TestAlternatePrint
func TestAlternatePrint(t *testing.T) {
AlternatePrint1()
AlternatePrint2()
}
// Method 1
func AlternatePrint1() {
fmt.Println(" Method 1, The codes of the two coroutines are different ")
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)
// Wait for the execution of the cooperation process to complete
time.Sleep(time.Second * 1)
}
// Method 2
func AlternatePrint2() {
fmt.Println(" Method 2, Two go The program code is exactly the same ")
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 {
// Get executive power
<-chs[i]
// Execute code
fmt.Printf("go cheng %d:%d\r\n", i, start)
if (i+1)%N == 0 {
fmt.Println("")
}
start++
// Give other programs the right to execute
chs[(i+1)%N] <- struct{}{}
}
}(i)
}
// Give it to 1 The right to execute a contract , The first 1 The sequence number of the coroutines is 0
chs[0] <- struct{}{}
// Wait for the execution of the cooperation process to complete
time.Sleep(time.Second * 1)
// Take back the last 1 individual go The executive power of Cheng
<-chs[POOL%N]
}
knock go test -v -test.run TestAlternatePrint command , give the result as follows :

版权声明
本文为[Fuda Dajia architect's daily question]所创,转载请带上原文链接,感谢
边栏推荐
- Some operations kept in mind by the front end foundation GitHub warehouse management
- 【涂鸦物联网足迹】涂鸦云平台全景介绍
- Exclusive interview with Alibaba cloud database for 2020 PostgreSQL Asia Conference: Zeng Wenjing
- How to prepare for the system design interview
- Understanding formatting principles
- JVM memory allocation - xms128m - xmx512m - XX: permsize = 128M - XX: maxpermsize = 512M
- How does cglib implement multiple agents?
- NAND FLASH的接口控制设计
- Application insights application insights use application maps to build request link views
- STM32F030F4P6兼容灵动微MM32F031F4P6
猜你喜欢

递归、回溯算法常用数学基础公式

The Interpreter pattern of behavior pattern

Event monitoring problem

Elasticsearch database | elasticsearch-7.5.0 application construction

移动端像素适配方案

The memorandum model of behavior model

The 4th China BIM (digital construction) manager Summit Forum will be held in Hangzhou in 2020

An article will introduce you to HTML tables and their main attributes

Small program introduction to proficient (2): understand the four important files of small program development

Git remote library rollback specified version
随机推荐
Qt音视频开发46-视频传输UDP版
An article will take you to understand CSS alignment
ES6 learning notes (2): teach you to play with class inheritance and class objects
Exclusive interview of guests at | 2020 PostgreSQL Asia Conference: Wang Tao
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
超高频RFID医疗血液管理系统应用
C calls SendMessage to refresh the taskbar icon (the icon does not disappear at the end of forcing)
事务的本质和死锁的原理
打工人好物——磨炼钢铁意志就要这样高效的电脑
The role of theme music in games
消防器材RFID固定资产管理系统
CloudQuery V1.2.0 版本发布
Unexpected element.. required element
2020-08-19:TCP是通过什么机制保障可靠性的?
1万辆!理想汽车召回全部缺陷车:已发生事故97起,亏损将扩大
An article taught you to download cool dog music using Python web crawler
How does cglib implement multiple agents?
Some operations kept in mind by the front end foundation GitHub warehouse management
The legality of IPFs / filecoin: protecting personal privacy from disclosure