Home > Article > Backend Development > How to solve the concurrent timer problem in Go language?
Concurrency timer issues in Go language refer to some concurrency-related issues that may occur when multiple goroutines need to use timers at the same time. In order to solve these problems, the Go language provides some mechanisms and techniques. This article will introduce these solutions in detail and give code examples.
package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(1 * time.Second) go func() { for { <-ticker.C fmt.Println("Tick") } }() time.Sleep(5 * time.Second) ticker.Stop() fmt.Println("Ticker stopped") }
In the above code, we create a ticker with a 1-second interval, and then continuously tick from the ticker's channel in a goroutine. Receive events in C and output "Tick". Wait 5 seconds in the main goroutine and then stop the ticker. Running this code will output the following results:
Tick Tick Tick Tick Tick Ticker stopped
package main import ( "context" "fmt" "time" ) func main() { ctx, cancel := context.WithCancel(context.Background()) go func() { for { select { case <-ctx.Done(): return case <-time.After(1 * time.Second): fmt.Println("Tick") } } }() time.Sleep(5 * time.Second) cancel() fmt.Println("Timer cancelled") }
In the above code, we first create a context object ctx with a cancellation function and pass it to the goroutine. In the goroutine, we use the select statement to listen to two channels: ctx.Done() and time.After(). When the ctx.Done() channel has a value, it means that the context has been canceled and we can exit the goroutine. When the time.After() channel has a value, it means that time is up and we print "Tick". In the main goroutine, we wait 5 seconds and then call the cancel() function to cancel the timer. Running this code will output the following results:
Tick Tick Tick Tick Tick Timer cancelled
package main import ( "fmt" "sync" "time" ) func main() { var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() time.Sleep(2 * time.Second) fmt.Println("Timer 1 finished") }() go func() { defer wg.Done() time.Sleep(3 * time.Second) fmt.Println("Timer 2 finished") }() wg.Wait() fmt.Println("All timers finished") }
In the above code, we use sync.WaitGroup to wait for the end of two timers. In the goroutine of each timer, we use the defer keyword to call wg.Done() at the end of the function, indicating that the current goroutine has ended. In the main goroutine, we call wg.Wait() to wait for all timers to end. Running this code will output the following results:
Timer 1 finished Timer 2 finished All timers finished
Summary:
This article introduces three solutions to the concurrent timer problem in the Go language, namely using time.Ticker, context.Context and sync.WaitGroup . Through code examples, we explain in detail the usage and precautions of each scheme. These solutions can help developers better handle issues related to concurrent timers and improve code reliability and performance.
The above is the detailed content of How to solve the concurrent timer problem in Go language?. For more information, please follow other related articles on the PHP Chinese website!