Golang Concurrent Programming Practical Experience Sharing: Using Goroutines to Improve Program Stability
Introduction:
In today's highly concurrent Internet era, writing stable and efficient multi-threaded programs has become particularly important. As a development language, Golang has powerful concurrent programming capabilities, and the Goroutines mechanism is an important part of its concurrent programming. In this article, we will share some experience and techniques of concurrent programming in Golang, and show through sample code how to use Goroutines to improve the stability of the program.
Sample code:
package main import ( "fmt" "time" ) func main() { for i := 0; i < 10; i++ { go printHello(i) } time.Sleep(time.Second) } func printHello(i int) { fmt.Println("Hello from Goroutine", i) }
In the above example, we define a function named printHello
, which prints out "Hello from Goroutine" information. In the main
function, we create 10 Goroutines using a loop and call the printHello
function. With the go
keyword, we start new Goroutines and make them run concurrently. At the end of the main
function, we use the time.Sleep
function to wait for all Goroutines to finish executing.
Sample code:
package main import ( "fmt" "sync" ) var ( counter int wg sync.WaitGroup mutex sync.Mutex ) func main() { for i := 0; i < 10; i++ { wg.Add(1) go increment() } wg.Wait() fmt.Println("Counter:", counter) } func increment() { mutex.Lock() defer mutex.Unlock() counter++ wg.Done() }
In the above example, we defined a global variable named counter
and created a mutex lockmutex
and a waiting group wg
. In the increment
function, we use mutex.Lock()
and mutex.Unlock()
to lock and unlock. This ensures that only one Goroutine can access the critical section code at a time, avoiding resource competition issues.
Sample code:
package main import ( "fmt" "time" ) func main() { ch := make(chan string) go sendData(ch) go receiveData(ch) time.Sleep(time.Second) } func sendData(ch chan<- string) { ch <- "Hello" ch <- "World" close(ch) } func receiveData(ch <-chan string) { for msg := range ch { fmt.Println(msg) } }
In the above example, we created a string type channelch
through the make
function . In the sendData
function, we sent two messages to the channel ch
and closed the channel through the close
function. In the receiveData
function, we use the range
keyword to traverse the information in the channel and print it out.
Through the use of channels, different Goroutines can safely conduct two-way communication, avoiding the problem of shared memory and improving the stability of the program.
Summary:
Through the introduction of this article, we have learned about Goroutines, the concurrent programming mechanism in Golang, and demonstrated through sample code how to use Goroutines to improve the stability of the program. In the actual development process, by making full use of Goroutines to implement concurrent execution functions, while avoiding resource competition and correctly handling communication between coroutines, efficient and stable multi-threaded programs can be written. I hope this article will be helpful to everyone in terms of practical experience in concurrent programming in Golang.
The above is the detailed content of Golang concurrent programming practical experience sharing: using Goroutines to improve program stability. For more information, please follow other related articles on the PHP Chinese website!