Middle — Middle+
Explain the concept of using a thread pool for task processing in a multithreaded application.
sobes.tech AI
Answer from AI
A worker pool is a mechanism that allows limiting the number of concurrently running threads (goroutines in Go) for task processing. Instead of creating a new goroutine for each task, tasks are placed in a queue, and a fixed number of workers (goroutines) process them sequentially. This helps control resource usage, avoid excessive goroutine creation, and improve performance.
In Go, a pool can be implemented using a task channel and a fixed number of worker goroutines that read tasks from the channel and execute them:
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
// process task j
results <- j * 2
}
}
func main() {
jobs := make(chan int, 100)
results := make(chan int, 100)
// Launch 3 workers
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
// Send 5 tasks
for j := 1; j <= 5; j++ {
jobs <- j
}
close(jobs)
// Collect results
for a := 1; a <= 5; a++ {
fmt.Println(<-results)
}
}
This approach allows efficient management of concurrency and application resources.