当前位置:网站首页>Write an example of goroutine and practice Chan at the same time
Write an example of goroutine and practice Chan at the same time
2022-06-27 06:38:00 【JeffyGao】
sketch
It's essentially a producer consumer model
- By controlling goroutine Number , Prevent inflation
- demand :
– Calculate the sum of the digits of a number , For example, digital 123, The result is 1+2+3=6
– Randomly generated numbers for calculation
Code :
package main
import (
"fmt"
"math/rand"
"time"
)
// producer
type Job struct {
Id int // Number
RandNum int // The resulting random value
}
// consumer
type Result struct {
job *Job // Incoming object instance why?
sum int // Sum value
}
// Create a work pool
// Parameters 1: Open several cooperation programs
func createPool(num int, jobChan chan *Job, resultChan chan *Result) {
// According to the number of processes To run
for i := 0; i < num; i++ {
go func(jC chan *Job, rC chan *Result) {
// Perform an operation
// Traverse job All data of the pipeline , Add additivity
for job := range jC {
// Random numbers come in
r_num := job.RandNum
// Each bit of random number is added
// sum The sum of digits returns the value
var sum int = 0
for r_num != 0 {
tmp := r_num % 10
sum += tmp
r_num /= 10
}
// Want to put the results in Result
r := &Result{
job: job,
sum: sum,
}
// Calculation results to chan
rC <- r
}
}(jobChan, resultChan)
}
}
func main() {
// need 2 individual chan Used to pass in numbers and evaluation
// 1. job The Conduit
jobChan := make(chan *Job, 128)
// 2. result The Conduit
resultChan := make(chan *Result, 128)
// 3. Create a work pool
createPool(64, jobChan, resultChan)
// 4. utilize goroutine Print data
go func(ch chan *Result) {
// Traversal result pipeline printing
for result := range ch {
fmt.Printf("job id: %v\trandnum: %v\tresult: %v\n", result.job.Id,
result.job.RandNum, result.sum)
}
}(resultChan)
var id int = 0
var count int = 20 // Only calculate 20 Group data
// Loop creation job, Input to pipeline
for count > 0 {
time.Sleep(time.Second) // Otherwise, the cooperation process will run 20 Times too fast
count--
id++
// Generate random number
r_num := rand.Int() // Here is generating a nonnegative number , [0, 2^64)
job := &Job{
Id: id,
RandNum: r_num,
}
jobChan <- job
}
}
边栏推荐
- 飞行器翼尖加速度和控制面的MPC控制
- Once spark reported an error: failed to allocate a page (67108864 bytes), try again
- Redis 缓存穿透、缓存击穿、缓存雪崩
- 使用CSDN 开发云搭建导航网站
- Change the status to the corresponding text during MySQL query
- 快速实现蓝牙iBeacn功能详解
- Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
- JVM object composition and storage
- Download CUDA and cudnn
- 日期 数据库日期 字符串 之间互相转换
猜你喜欢

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

Us camera cloud service scheme: designed for lightweight video production scenes

Fast implementation of thread mesh networking

Thinking technology: how to solve the dilemma in work and life?

JVM常用指令

2022 cisp-pte (I) document contains

分数阶PID控制

JVM class loading mechanism

Dev++ environment setting C language keyword display color

浅谈GPU:历史发展,架构
随机推荐
Redis 缓存穿透、缓存击穿、缓存雪崩
小米面试官:听你说精通注册中心,我们来聊 3 天 3 夜
thrift
Spark SQL common time functions
【LeetCode】Day90-二叉搜索树中第K小的元素
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
Unrecognized VM option ‘‘
Block level elements & inline elements
日期 数据库日期 字符串 之间互相转换
2018 mathematical modeling competition - special clothing design for high temperature operation
0.0.0.0:x的含义
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
路由器和交换机的区别
乐观事务和悲观事务
Altium designer 19 device silk screen label position shall be placed uniformly in batches
HTAP Quick Start Guide
JVM常用指令
winow10安装Nexus nexus-3.20.1-01
Proxy-Reflect使用详解
The fourth question of the 299th weekly match 6103 Minimum fraction of edges removed from the tree