当前位置:网站首页>Go concurrency and channel
Go concurrency and channel
2022-04-23 15:41:00 【The end of the world and you】
Go Concurrency and channels
1.Go Concurrent
Go Languages support concurrency , We just need to pass go Keyword to open goroutine
that will do .
goroutine
It's a lightweight thread ,goroutine
The dispatch of is made by Golang Managed at run time .
goroutine
Grammar format :
go Function name ( parameter list )
Go Allow to use go Statement to open a new runtime thread , namely
goroutine
, With a different 、 The newly createdgoroutine
To execute a function . All in the same programgoroutine
Share the same address space .
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
---------------------------------
Output :
hello
world
world
hello
hello
world
world
hello
hello
Execute the above code , You'll see the output hello and world There is no fixed order . Because they are two
goroutine
In execution
2. passageway (channel)
passageway (channel
) Is a data structure used to transfer data .
Channels can be used for two goroutine
By passing a value of the specified type to synchronize operation and communication between . The operator <-
Used to specify the direction of the channel , To send or receive . If no direction is specified , It's a two-way channel .
ch <- v // hold v Send to channel ch
v := <-ch // from ch receive data
// And assign values to v
Be careful : By default , The channel is unbuffered . The sender sends data , At the same time, there must be corresponding receiving data at the receiving end .
example 1: Through two goroutine
To sum up the numbers , stay goroutine
After the calculation , It calculates the sum of the two results
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // hold sum Send to channel c
}
func main() {
s := []int{
7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // From the tunnel c In the receiving
fmt.Println(x, y, x+y)
}
-------------------------------------
Output :-5 17 12
Channel buffer
Channels can set buffers , adopt make
The second parameter of specifies the buffer size :
ch := make(chan int, 100)
The channel with buffer allows the data transmission of the sender and the data acquisition of the receiver to be in an asynchronous state , In other words, the data sent by the sender can be put in the buffer , You can wait for the receiver to get the data , Instead of requiring the receiver to get the data immediately .
But because the size of the buffer is limited , So there must be a receiver to receive data , Otherwise the buffer is full , The data sender can't send any more data .
Be careful : If the channel is not buffered , The sender blocks until the receiver receives the value from the channel . If the channel is buffered , The sender blocks until the value is copied into the buffer ; If the buffer is full , It means waiting until a receiver gets a value . The receiver will block until it has a value to receive .
example 2: Using buffers
package main
import "fmt"
func main() {
// Here we define a buffered channel that can store integer types
// The buffer size is 2
ch := make(chan int, 2)
// because ch It's a buffered channel , We can send two data at the same time
// Instead of having to synchronize reading data immediately
ch <- 1
ch <- 2
// Get these two data
fmt.Println(<-ch)
fmt.Println(<-ch)
}
-----------------------------
Output :
1
2
Go Traversing channels and closing channels
Go adopt range
Keyword to traverse the read data , Similar to array or slice . The format is as follows :
v, ok := <-ch
If the channel does not receive data ok for false, Then the channel can be used close()
Function to close .
example 3:
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
// range Function traverses each data received from the channel , because c After sending 10 individual
// After the data, the channel is closed , So here we are range Function is receiving 10 Data
// And then it's over . If the top c The channel doesn't close , that range Functions don't
// It's over , So in the receiving section 11 It's blocked when it comes to data .
for i := range c {
fmt.Println(i)
}
}
-----------------------------------------
Output :
0
1
1
2
3
5
8
13
21
34
Copyright notice : This tutorial is based on the rookie tutorial
版权声明
本文为[The end of the world and you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231533523936.html
边栏推荐
- Mobile finance (for personal use)
- Cookie&Session
- Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
- GFS distributed file system (Theory)
- 字符串最后一个单词的长度
- 码住收藏▏软件测试报告模板范文来了
- How to test mobile app?
- 网站建设与管理的基本概念
- PHP PDO ODBC将一个文件夹的文件装载到MySQL数据库BLOB列,并将BLOB列下载到另一个文件夹
- 自动化测试框架常见类型▏自动化测试就交给软件测评机构
猜你喜欢
Cookie&Session
Codejock Suite Pro v20. three
Multitimer V2 reconstruction version | an infinitely scalable software timer
大型互联网为什么禁止ip直连
pgpool-II 4.3 中文手册 - 入门教程
Basic concepts of website construction and management
Openstack theoretical knowledge
Neodynamic Barcode Professional for WPF V11. 0
Advantages, disadvantages and selection of activation function
What if the server is poisoned? How does the server prevent virus intrusion?
随机推荐
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
Application of Bloom filter in 100 million flow e-commerce system
Date date calculation in shell script
Machine learning - logistic regression
现在做自媒体能赚钱吗?看完这篇文章你就明白了
【递归之数的拆分】n分k,限定范围的拆分
群体智能自主作业智慧农场项目启动及实施方案论证会议
Multitimer V2 reconstruction version | an infinitely scalable software timer
What if the package cannot be found
APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
Special analysis of China's digital technology in 2022
Mysql database explanation (IX)
Upgrade MySQL 5.1 to 5.67
怎么看基金是不是reits,通过银行购买基金安全吗
时序模型:门控循环单元网络(GRU)
shell脚本中的DATE日期计算
Recommended search common evaluation indicators
Mysql database explanation (10)
ICE -- 源码分析
大型互联网为什么禁止ip直连