当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- What the hell is fastthreadlocal? The existence of ThreadLocal!!
- 小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
- Python basic variable type -- list analysis
- Detect certificate expiration script
- To Lianyun analysis: why is IPFs / filecoin mining so difficult?
- Exclusive interview of guests at | 2020 PostgreSQL Asia Conference: Wang Tao
- html+ vue.js Implementing paging compatible IE
- An article will introduce you to HTML tables and their main attributes
- 事务的本质和死锁的原理
- ES6 learning notes (4): easy to understand the new grammar of ES6
猜你喜欢
谷歌浏览器实现视频播放加速功能
Some operations kept in mind by the front end foundation GitHub warehouse management
Common syntax corresponding table of mongodb and SQL
STM32F030F4P6兼容灵动微MM32F031F4P6
行为型模式之解释器模式
How to play sortable JS vuedraggable to realize nested drag function of forms
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
磁存储芯片STT-MRAM的特点
Es create a new index database and copy the old index library, practice pro test effective!
How much disk space does a file of 1 byte actually occupy
随机推荐
Some operations kept in mind by the front end foundation GitHub warehouse management
Exclusive interview of guests at | 2020 PostgreSQL Asia Conference: Wang Tao
What are the highlights of Huawei mate 40 series with HMS?
Zero basis to build a web search engine of its own
Common mathematical basic formulas of recursive and backtracking algorithms
Novice guidance and event management system in game development
Call analysis of start method in JNI thread and callback analysis of run method
2020-09-03:裸写算法:回形矩阵遍历。
[elastic search engine]
An article taught you to download cool dog music using Python web crawler
An article will take you to understand CSS alignment
This project allows you to quickly learn about a programming language in a few minutes
【涂鸦物联网足迹】物联网基础介绍篇
Using an example to understand the underlying processing mechanism of JS function
Method of code refactoring -- Analysis of method refactoring
Unexpected element.. required element
How does cglib implement multiple agents?
移动端像素适配方案
行为型模式之解释器模式
To Lianyun analysis: why is IPFs / filecoin mining so difficult?