当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Qt音视频开发46-视频传输UDP版
- The isolation level of transaction and its problems
- 小熊派开发板实践:智慧路灯沙箱实验之真实设备接入
- Contract trading system development | construction of smart contract trading platform
- Understanding formatting principles
- This project allows you to quickly learn about a programming language in a few minutes
- An article taught you to use HTML5 SVG tags
- With this artifact, quickly say goodbye to spam messages
- Why is the LS command stuck when there are too many files?
- 2020-08-20:GO语言中的协程与Python中的协程的区别?
猜你喜欢

Unexpected element.. required element

STM32F030C6T6兼容替换MM32SPIN05PF

How does filecoin's economic model and future value support the price of fil currency breaking through thousands

Nodejs中使用jsonwebtoken(JWT)生成token的场景使用

How about small and medium-sized enterprises choose shared office?

STM32F030K6T6兼容替换灵动MM32F031K6T6

意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……

Road to simple HTML + JS to achieve the most simple game Tetris

An article takes you to understand CSS3 picture border

Introduction to the development of small game cloud
随机推荐
How to prepare for the system design interview
Code generator plug-in and creator preform file analysis
DC-1 target
An article will introduce you to CSS3 background knowledge
Common syntax corresponding table of mongodb and SQL
ES6 learning notes (2): teach you to play with class inheritance and class objects
Git rebase is in trouble. What to do? Waiting line
Ora-02292: complete constraint violation (midbjdev2.sys_ C0020757) - subrecord found
STM32F030C6T6兼容替换MM32SPIN05PF
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
轻量型 GPU 应用首选 京东智联云推出 NVIDIA vGPU 实例
An article takes you to understand CSS3 picture border
Road to simple HTML + JS to achieve the most simple game Tetris
An article will take you to understand CSS alignment
实验一
Method of code refactoring -- Analysis of method refactoring
Windows 10 蓝牙管理页面'添加蓝牙或其他设备'选项点击无响应的解决方案
What kind of music do you need to make for a complete game?
Junit测试出现 empty test suite
Take you to learn the new methods in Es5