This is the 8th day of my participation in Gwen Challenge
Before learning coroutine, you can first understand the process, thread related content
goroutine
In Go, there are no threads, only coroutines called Goroutines.
Concurrency in GO is determined by GO itself, and is transparent to the developer, who only needs to be told to start a few Goroutines when coding and doesn’t care about the rest
Compared to threads:
- Coroutines are lighter, and a single program can launch thousands of Goroutines
- Coroutines are executed by the Go Runtime, and threads are executed by the operating system.
Start coroutines: Start with the keyword go
go function()
Copy the code
channel
To solve the communication between multiple coroutines, mainly through the use of channel channels
Declare a channel using the make function
// Declare a channel variable whose data are strings
ch := make(chan string)
Copy the code
The keyword chan, which represents the channel type, is a collection type,
There are only two types of chan operations:
Receive: Gets the value in chan with the operator < -chan
Send: Sends a value to chan and places the value in chan with the operator chan<-
A channel is similar to a goroutine channel. One channel sends data to the channel and the other one fetches data from the channel
-
There is no buffer channel
- Declare a channel with no capacity, only to transmit data
- The send and receive operations of an unbuffered channel occur simultaneously, also called a synchronous channel
-
Have a buffer channel
-
Create a buffered channel with channel capacity specified
ch := make(chan int.5) Copy the code
-
A buffered channel has a buffered queue inside it
-
The send operation inserts an element to the end of the queue and, when the queue is full, blocks and waits until a coroutine receives data from the queue, freeing up queue space
-
The receive operation takes an element from the head of the queue and removes it from the queue, and when the queue is empty blocks until a coroutine sends data to the queue
ch := make(chan int.6) cap(ch) // Get the chan length close(ch) / / close chan Copy the code
-
-
A one-way channel
Scenario: Restrict a channel to receive only but not send, or send only but not receive
select
Similar to Switch, but a little different. Select can only be used for channels, so SELECT is mentioned here
A select case is followed by a channel operation, not a judgment condition.
-
Select statements can only be used for read and write operations on channels
-
The case condition of select is concurrent execution. The successful case is executed first. If multiple returns are returned at the same time, one case is randomly selected for execution
-
If there is an read or write operation in a case condition that has a channel value of nil, the branch is ignored
-
For an empty SELECT {}, a deadlock is caused
-
Select {} for may cause high CPU usage
ch := make(chan int , 10) for i := 0; i < size; i++ { ch <- 1 } select { case 1= =1: // An error is reported at runtime here fmt.Println("equal") case v:= <-ch fmt.Print(v) default: fmt.Println("none")}select { case 1= =1: // An error is reported at runtime here fmt.Println("equal") case v:= <-ch fmt.Print(v) case b:= <-ch fmt.Print(b) default: fmt.Println("none")}Copy the code