目录
1.并发简介
并发是同时处理多个任务的能力。在 Go 中,并发性是一等公民,内置于该语言的核心设计中。 Go 的并发方法基于通信顺序进程(CSP),该模型强调进程之间的通信而不是共享内存。
2.并发与并行:
Go 例程支持并发,这是独立执行进程的组合。
如果系统有多个 CPU 核心并且 Go 运行时安排 go 例程并行运行,则可能会发生并行(同时执行)。
3。 Go 例程:
并发的构建块是 Go 例程,是由 Go 运行时管理的轻量级线程。它是与其他函数或方法同时运行的函数或方法。 Go 例程是 Go 并发模型的基础。
主要特征:
创建 Go 例程:
要启动 go 例程,只需使用 go 关键字,后跟函数调用:
go functionName()
或者使用匿名函数:
go func() { // function body }()
Go-routine 调度:
通讯与同步:
示例及说明:
package main import ( "fmt" "time" ) func printNumbers() { for i := 1; i <= 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Printf("%d ", i) } } func printLetters() { for i := 'a'; i <= 'e'; i++ { time.Sleep(150 * time.Millisecond) fmt.Printf("%c ", i) } } func main() { go printNumbers() go printLetters() time.Sleep(2 * time.Second) fmt.Println("\nMain function finished") }
说明:
Goroutine 生命周期:
最佳实践:
带有 go 例程解释的简单示例
package main import ( "fmt" "time" ) // printNumbers is a function that prints numbers from 1 to 5 // It will be run as a goroutine func printNumbers() { for i := 1; i <= 5; i++ { time.Sleep(500 * time.Millisecond) // Sleep for 500ms to simulate work fmt.Printf("%d ", i) } } // printLetters is a function that prints letters from 'a' to 'e' // It will also be run as a goroutine func printLetters() { for i := 'a'; i <= 'e'; i++ { time.Sleep(300 * time.Millisecond) // Sleep for 300ms to simulate work fmt.Printf("%c ", i) } } func main() { // Start printNumbers as a goroutine // The 'go' keyword before the function call creates a new goroutine go printNumbers() // Start printLetters as another goroutine go printLetters() // Sleep for 3 seconds to allow goroutines to finish // This is a simple way to wait, but not ideal for production code time.Sleep(3 * time.Second) // Print a newline for better formatting fmt.Println("\nMain function finished") }
4.频道:
通道是 Go 中的一项核心功能,它允许 go 例程相互通信并同步执行。它们为一个 go 例程提供了一种将数据发送到另一个 go 例程的方法。
频道的目的
Go 中的通道有两个主要用途:
a) 通信:它们允许 goroutine 相互发送和接收值。
b) 同步:它们可用于跨 Goroutine 同步执行。
创建:使用 make 函数创建通道:
ch := make(chan int) // Unbuffered channel of integers
发送:使用
ch <- 42 // Send the value 42 to the channel
Receiving: Values are received from a channel using the <- operator:
value := <-ch // Receive a value from the channel
Types of Channels
a) Unbuffered Channels:
ch := make(chan int) go func() { ch <- 42 // This will block until the value is received }() value := <-ch // This will receive the value
b) Buffered Channels:
ch := make(chan int, 2) ch <- 1 // Doesn't block ch <- 2 // Doesn't block ch <- 3 // This will block until a value is received
Channel Directions
Channels can be directional or bidirectional:
Example :
func send(ch chan<- int) { ch <- 42 } func receive(ch <-chan int) { value := <-ch fmt.Println(value) }
Closing Channels
Channels can be closed to signal that no more values will be sent:
close(ch)
Receiving from a closed channel:
If the channel is empty, it returns the zero value of the channel's type.
You can check if a channel is closed using a two-value receive:
value, ok := <-ch if !ok { fmt.Println("Channel is closed") }
Ranging over Channels
You can use a for range loop to receive values from a channel until it's closed:
for value := range ch { fmt.Println(value) }
Hey, Thank you for staying until the end! I appreciate you being valuable reader and learner. Please follow me here and also on my Linkedin and GitHub .
The above is the detailed content of Concurrency in Go: From Basics to Advanced Concepts. For more information, please follow other related articles on the PHP Chinese website!