当前位置:网站首页>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 is the tensor in tensorflow?
- Metersphere developer's Manual
- 谷歌浏览器实现视频播放加速功能
- Zhou Jie: database system of East China Normal University
- 实验一
- The legality of IPFs / filecoin: protecting personal privacy from disclosure
- 磁存储芯片STT-MRAM的特点
- Detect certificate expiration script
- Pn8162 20W PD fast charging chip, PD fast charging charger scheme
- 递归、回溯算法常用数学基础公式
猜你喜欢

Git rebase is in trouble. What to do? Waiting line

An article will introduce you to CSS3 background knowledge

大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术

How much disk space does a file of 1 byte actually occupy

Those who have worked in China for six years and a million annual salary want to share these four points with you

2020-08-24:什么是小文件?很多小文件会有什么问题?很多小文件怎么解决?(大数据)

A small goal in 2019 to become a blog expert of CSDN

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

2020-08-17:详细说下数据倾斜怎么解决?
![[byte jumps, autumn recruitment Posts open] ohayoo! Don't leave after school, I want to ask you to play games!!!](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[byte jumps, autumn recruitment Posts open] ohayoo! Don't leave after school, I want to ask you to play games!!!
随机推荐
意外的元素..所需元素..
How to make characters move
What kind of music do you need to make for a complete game?
Ora-02292: complete constraint violation (midbjdev2.sys_ C0020757) - subrecord found
An article will take you to understand CSS3 fillet knowledge
Junit测试出现 empty test suite
How does cglib implement multiple agents?
Application insights application insights use application maps to build request link views
Vue communication and cross component listening state Vue communication
How does filecoin's economic model and future value support the price of fil currency breaking through thousands
Erd-online free online database modeling tool
STM32F030C6T6兼容替换MM32SPIN05PF
2020-08-30:裸写算法:二叉树两个节点的最近公共祖先。
A concise tutorial for Nacos, ribbon and feign
Qt音视频开发46-视频传输UDP版
【涂鸦物联网足迹】涂鸦云平台全景介绍
Open source a set of minimalist front and rear end separation project scaffold
Introduction to Huawei cloud micro certification examination
ES中删除索引的mapping字段时应该考虑的点
行为型模式之解释器模式