当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- ES中删除索引的mapping字段时应该考虑的点
- Common mathematical basic formulas of recursive and backtracking algorithms
- 2020-08-24:什么是小文件?很多小文件会有什么问题?很多小文件怎么解决?(大数据)
- Interviewer: how about shardingsphere
- What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
- Stickinengine architecture 11 message queue
- Introduction to Huawei cloud micro certification examination
- 2020-08-18:介绍下MR过程?
- What is the tensor in tensorflow?
- 轻量型 GPU 应用首选 京东智联云推出 NVIDIA vGPU 实例
猜你喜欢
Event monitoring problem
Exclusive interview with Alibaba cloud database for 2020 PostgreSQL Asia Conference: Zeng Wenjing
An article will take you to understand SVG gradient knowledge
[elastic search engine]
Common syntax corresponding table of mongodb and SQL
2020-08-18:介绍下MR过程?
2020-08-17:详细说下数据倾斜怎么解决?
An article will take you to understand CSS3 fillet knowledge
An article will introduce you to HTML tables and their main attributes
Detect certificate expiration script
随机推荐
Markdown tricks
[forward] how to view UserData in Lua
An article will take you to understand CSS alignment
An article takes you to understand CSS gradient knowledge
#JVM 类加载机制
CloudQuery V1.2.0 版本发布
Those who have worked in China for six years and a million annual salary want to share these four points with you
An article taught you to use HTML5 SVG tags
The legality of IPFs / filecoin: protecting personal privacy from disclosure
Novice guidance and event management system in game development
[learning] interface test case writing and testing concerns
上海巨微专用蓝牙广播芯片
The 4th China BIM (digital construction) manager Summit Forum will be held in Hangzhou in 2020
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
Application insights application insights use application maps to build request link views
GitHub: the foundation of the front end
image operating system windows cannot be used on this platform
Big data processing black Technology: revealing the parallel computing technology of Pb level data warehouse gaussdb (DWS)
Common syntax corresponding table of mongodb and SQL
NAND FLASH的接口控制设计