当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
- 实验一
- An article will take you to understand CSS3 fillet knowledge
- Zero basis to build a web search engine of its own
- This project allows you to quickly learn about a programming language in a few minutes
- C and C / C + + mixed programming series 5 - GC collaboration of memory management
- ES6 learning notes (5): easy to understand ES6's built-in extension objects
- An article takes you to understand CSS3 picture border
- JVM memory allocation - xms128m - xmx512m - XX: permsize = 128M - XX: maxpermsize = 512M
- 【涂鸦物联网足迹】物联网基础介绍篇
猜你喜欢
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
This project allows you to quickly learn about a programming language in a few minutes
Those who have worked in China for six years and a million annual salary want to share these four points with you
Zero basis to build a web search engine of its own
非易失性MRAM存储器应用于各级高速缓存
An article will take you to understand CSS alignment
#JVM 类加载机制
Interviewer: how about shardingsphere
Elasticsearch database | elasticsearch-7.5.0 application construction
2020-09-04:函数调用约定了解么?
随机推荐
2020 database technology conference helps technology upgrade
Unity performance optimization
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
Exclusive interview with Alibaba cloud database for 2020 PostgreSQL Asia Conference: Zeng Wenjing
The legality of IPFs / filecoin: protecting personal privacy from disclosure
window系统 本机查找端口号占用方法
Count the number of project code lines
The memorandum model of behavior model
The native API of the future trend of the front end: web components
Using an example to understand the underlying processing mechanism of JS function
[byte jumps, autumn recruitment Posts open] ohayoo! Don't leave after school, I want to ask you to play games!!!
2020-09-04:函数调用约定了解么?
What kind of music do you need to make for a complete game?
Understanding formatting principles
C language I blog assignment 03
Markdown tricks
Some operations kept in mind by the front end foundation GitHub warehouse management
Elasticsearch database | elasticsearch-7.5.0 application construction
2020-08-24:什么是小文件?很多小文件会有什么问题?很多小文件怎么解决?(大数据)
Python basic variable type -- list analysis